C++之const

本文探讨了C++中const关键字的设计目的及其在实际应用中的规则。const对象只能调用const成员函数,而const成员函数只能修改mutable修饰的数据成员。详细列举了不同情况下const对象与non-const对象调用成员函数的正确性,并提到了volatile变量,它是用来指示其值可能意外改变的,防止编译器进行优化。最后提到了pthread_mutex_lock,暗示在多线程环境下对const对象的保护可能涉及到的同步机制。
摘要由CSDN通过智能技术生成

 const设计的本质是为了保护成员不被修改,但是在生产环境下会遇见许多情形,如下:

#include<iostream>
using namespace std;
class data{
    public:
        void get(){
            cout<<"data::get()"<<endl;
        }
};
class test{
    public:
        test(){
            a=1;
            b=10;
        }
        void show(int i){
            cout<<"show(int i)"<<endl;
        }
        //void show(const int i){//传值调用const不能实现调用,编译错误
        //    cout<<"show(const int i)"<<endl;
        //}
        void print(int &i){
            cout<<"print(int &i)"<<endl;
        }
        void print(const int& i){//利用const参数完成重载
            cout<<"print(const int& i)"<<endl;
        }
        void fun(){
            cout<<"fun()"<<endl;
        }
        void fun() const{//利用const完成函数重载
            cout<<"fun() const"<<endl;
            cout<<++a<<endl;//const函数能修改mutable变量,2
            cout<<b<<endl;//const函数可以访问非const成员变量但不能修改,10
            //++b;//const函数不能修改成员,编译错误
            //show(1);//const函数不能调用非const成员,编译错误
        }
        void nonConstFun(){
            cout<<"nonConstFun()"<<endl;
        }
        void getdata() const{
            //D.get();//const函数访问非const成员对象,编译错误
            const_cast<data*>(&D)->get();
            D2.get();
        }
    public:
        mutable int a;
        int b;
        data D;
        mutable data D2;
};
int main(){
    test one;
    const test two;
    int i=1;
    const int j=1;
    one.show(i);//对于非指针和非引用来说是传值调用故const不能重载,show(int i)
    one.show(j);//show(int i )
    one.print(i);//非const的引用参数重载print(int &i),指针类似
    one.print(j);//const引用参数的重载print(const int& i)
    one.fun();//非const对象优先调用非const成员函数 fun()
    two.fun();//const对象调用const成员函数fun() const
    //two.nonConstFun();//const对象调用非const成员,编译出错,const对象只能调用const成员函数,理由是防止成员函数篡改成员
    two.getdata();//data::get()
    return 0;
}


执行结果:

show(int i)
show(int i)
print(int &i)
print(const int& i)
fun()
fun() const
2
10
data::get()
data::get()



总结:

const对象因为数据成员不能被修改所以只能调用const成员函数。

const成员函数成为“只读”函数所以只能调用const成员函数。某些特殊比如mutable和const_cast情形另当别论

对象.  成员函数 
     对象                 成员函数       对/错 
1、  const            const              对 
2、  const            non-const       错 
3、  non-const     const              对 
4、  not-const      non-const      对


   成员函数调用成员函数 
     成员函数           成员函数       对/错 
5、  const             const            对 
6、  const             non-const     错 
7、  non-const      const            对 
8、  non-const      non-const     对

一直没搞清楚volatile变量附上:volatile变量是说这变量可能会被意想不到地改变,这样,编译器就不会去假设这个变量的值了。精确地说就是,优化器在用到这个变量时必须每次都小心地重新读取这个变量的值,而不是使用保存在寄存器里的备份。

#include<iostream>
#include<unistd.h>
#include<pthread.h>
using namespace std;
class test{
    public:
        void show() const{
            pthread_mutex_lock(&mutex);
            cout<<"pthread_mutex_lock"<<endl;
            pthread_mutex_unlock(&mutex);
        }
        test(){
            pthread_mutex_init(&mutex,NULL);
        }
    private:
        //pthread_mutex_t mutex;//编译错误
        mutable pthread_mutex_t mutex;//为了能让const成员函数能使用互斥量最好将互斥量都声明为mutable
};
int main(){
    test one;
    one.show();
    return 0;
}

输出:

pthread_mutex_lock

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值