C/C++,常对象 - 保护共享数据

定义常对象的作用是为了保证该对象中数据成员的值不能被修改。

定义常对象的一般形式为

类名 const 对象名 [(实参表)];

或者

const 类名 对象名 [(实参表)];

编辑如下源文件student_change.cpp:

#include <iostream>
using namespace std;

class Student
{
    public:
        Student(int n, float s):num(n), score(s){}
        void change(int n, float s){num = n; score = s;}
        void display(){cout << num << " " << score << endl;}

    private:
        int num;
        float score;
};

int main()
{
    Student s(101, 78.5);
    s.display();
    s.change(101, 80.5);
    s.display();

    return 0;
}

编译运行结果:
这里写图片描述


修改上述源文件,将 s 声明为常对象:

#include <iostream>
using namespace std;

class Student
{
    public:
        Student(int n, float s):num(n), score(s){}
        void change(int n, float s){num = n; score = s;}
        void display(){cout << num << " " << score << endl;}

    private:
        int num;
        float score;
};

int main()
{
    const Student s(101, 78.5);
    s.display();
    s.change(101, 80.5);
    s.display();

    return 0;
}

编译结果:
这里写图片描述
通过编译时提示的错误信息可知:常对象不能调用该对象的普通成员函数。
这是为了防止普通成员函数会修改对象中数据成员的值。


修改上述源文件,将 s 的成员函数声明为常成员函数:

#include <iostream>
using namespace std;

class Student
{
    public:
        Student(int n, float s):num(n), score(s){}
        void change(int n, float s) const {num = n; score = s;}
        void display() const {cout << num << " " << score << endl;}

    private:
        int num;
        float score;
};

int main()
{
    const Student s(101, 78.5);
    s.display();
    s.change(101, 80.5);
    s.display();

    return 0;
}

编译结果:
这里写图片描述
对比第二次编辑时提示错误信息可知:常对象可以调用它的常成员函数,但是不能修改常对象中数据成员的值。


修改上述源文件,将常对象中的数据成员声明为mutable:

#include <iostream>
using namespace std;

class Student
{
    public:
        Student(int n, float s):num(n), score(s){}
        void change(int n, float s) const {num = n; score = s;}
        void display() const {cout << num << " " << score << endl;}

    private:
        mutable int num;
        mutable float score;
};

int main()
{
    const Student s(101, 78.5);
    s.display();
    s.change(101, 80.5);
    s.display();

    return 0;
}

编译运行结果:
这里写图片描述
通过编译运行结果可知:如果想要修改常对象中某个数据成员的值,则需要将该数据成员声明为mutable。


结合以上测试结果,可得出以下结论:

  1. 如果将一个对象声明为常对象,则该对象只能调用它的常成员函数,而不能调用该对象的普通成员函数;
  2. 常成员函数可以访问常对象中的数据成员,但不允许修改常对象中数据成员的值;
  3. 常成员函数可以修改声明为mutable(可变的)数据成员的值。

修改初始源文件,定义指向常对象的指针变量:

#include <iostream>
using namespace std;

class Student
{
    public:
        Student(int n, float s):num(n), score(s){}
        void change(int n, float s) {num = n; score = s;}
        void display() {cout << num << " " << score << endl;}

    private:
        int num;
        float score;
};

int main()
{
    Student s(101, 78.5);
    const Student *p = &s;
    s.display();
    p->change(101, 80.5);
    p->display();

    return 0;
}

编译结果:
这里写图片描述
通过编译时提示的错误信息可知:如果定义了一个指向常对象的指针变量,并使它指向一个非const的对象,则其指向的对象不能通过该指针变量改变。


修改上述源文件,定义指向对象的常指针变量:

#include <iostream>
using namespace std;

class Student
{
    public:
        Student(int n, float s):num(n), score(s){}
        void change(int n, float s) {num = n; score = s;}
        void display() {cout << num << " " << score << endl;}

    private:
        int num;
        float score;
};

int main()
{
    Student s(101, 78.5);
    Student * const p = &s;
    s.display();
    p->change(101, 80.5);
    p->display();

    return 0;
}

编译运行结果:
这里写图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值