C++ 中常量成员函数

class X
{
public:
X():buffer_(0),isComputed_(false){}
//...
void setBuffer()
{
     int *tmp = new int[MAX];
     delete []buffer_;
     buffer_=tmp;
}
void modifyBuffer(int index,int value) const //不道德!
{
     buffer_[index] = value;
}
int getValue() const
{
     if(! isComputed)
     {     
        computedValue_ = expensiveOperation();//错误!
        isComputed_ = true;//错误!
     }
     return computedValue_;
}
private:
     static int expensiveOperation();
     int *buffer_;
     bool isComputed_;
     int computedValue;
};

1、modifyBuffer可以被合法地标记为常量,因为它没有修改X对象,它只是修改X的buffer_成员所指向的一些数据。

这种做法是合法的,但很不道德。
2、getValue被标记为const ,但是改变了X对象,显然是错误的。

    对getVlue的有两种修改,一下作对比

想法一:


int getValue() const
{
     if(!isComputed_)
     {
         X *const aThis = const_cast<X *const>(this);//糟糕的念头!
         aThis->computedValue_ = expensiveOperation();
         aThis->isComputed_ = true;
     }
     return computedValue_;
}

评价:const 成员函数中不能对成员变量修改是因为this指针为const X * const this(非const成员函数this 指针为X * const this),

所以有“X *const aThis = const_cast<X *const>(this);”这一步的想法。千万要抵制这种错误的诱惑!

想法二:

将有关数据成员声明为mutable。问题轻松解决!


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值