条款3:尽可能的使用const

* 将某些东西声明为const可帮助编译器侦测出错误的用法。

* 假如当const和non-const成员函数有着相同的用法时,non-const的成员函数调用const版本可以避免代码重复


const出现在*左侧,表示被指物是常量;出现在*右侧,表示指针是常量。也可能同时出现在*两侧。

被指物是常量时,const写在类型的前后都是可以的

const int * num1 = 3;
int const * num2 = 3;

如果两个成员函数如果只是常量性不同,可以被重载

class ConstTest{
public:
    const char& operator[](std::size_t postion) const{
        //验证等其他操作
        std::cout<<"const"<<std::endl;
        return text[postion];
    }
    char& operator[](std::size_t postion){
        //验证等其他操作
        std::cout<<"no const"<<std::endl;
        return text[postion];
    }
private:
    std::string text{"hello"};
};

void print(const ConstTest& ctb){
    std::cout<<ctb[0]<<std::endl;
}

void print(ConstTest& ctb){
    std::cout<<ctb[0]<<std::endl;
}

int main()
{
    const ConstTest c1;
    print(c1);// const t

    ConstTest c2;
    print(c2);// no const t
    return 0;
}

避免const和no-const成员函数中的重复

由上述代码中的运算符重载函数可知,除了返回字符,还有验证等其他操作,两个函数中明显这部分是重复的,大部分人可能会抽离出这部分代码重新写封装成一个函数,让运算符重载函数调用,但无形之中也增加函数调用等等各种操作。解决办法如下:

class ConstTest{
public:
    const char& operator[](std::size_t postion) const{
        std::cout<<"const"<<std::endl;
        std::cout<<"验证等重复性工作"<<std::endl;
        return text[postion];
    }
    char& operator[](std::size_t postion){
        std::cout<<"非const-"<<std::flush;
        return const_cast<char&>(
        static_cast<const ConstTest&>(*this)[postion]
        );
    }
private:
    std::string text{"hello"};
};

void print(const ConstTest& ctb){
    std::cout<<ctb[0]<<std::endl;
}

void print(ConstTest& ctb){
    std::cout<<ctb[0]<<std::endl;
}

int main(int argc, char *argv[])
{
    const ConstTest c1;
    print(c1);

    ConstTest c2;
    print(c2);
}

为了解决代码重复的问题,用非const版本调用const版本代码。(非const调用const本身就是安全的,反之,则不是),上述代码中,首先将*this从非const类型静态类型转换(安全)转换为const类型,这是text[postion] 调用的是const版本的重载函数,调用完成返回时在利用 const_cast去掉const。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

搓搓程序狗

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值