c++常量成员函数详解
来源:互联网 发布:php导入excel带进度条 编辑:程序博客网 时间:2024/04/28 06:04
/*************************************************
* 常量成员函数的含义(成员函数 + const)
*
* 在类的 非 常量成员函数中,this指针类型是 X *const(指
* 针常量),而在 常量成员函数中,this指针类型是const X *const
* 不能被修改,这就是常量成员函数和非常量成员函数的区别.
*************************************************/
//例1
class X
{
private:
static int expensiveOperation();
int *buffer_;
bool isComputed_;
int computedValue_;
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_;
};
/*************************************************
* 如果我们需要在常量函数中修改类成员怎么办?
* 类的非静态变量数据成员可以声明为mutable,这将允许
* 它们的值可以被常量成员函数(当然也包括成员函数)所修改.
*************************************************/
//例2
class X
{
private:
static int expensiveOperation();
int *buffer_;
mutable bool isComputed_; //注意 mutable
mutable int computedValue_; //注意 mutable
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_;
};
/*************************************************
* 下面通过操作符的重载来深刻理解常量成员函数
*************************************************/
//例3
class X
{
public:
//...
int &operator [](int index); // ①
const int &operator [](int index) const; //②
//...
};
void main()
{
int i = 10;
X a;
a[5] = i; //重载 ①
const X b;
i = b[5]; //重载 ②
}
class X
{
public:
X operator + (const X &rightArg); //左边的参数是非常量
X operator + (const X &rightArg) const; //左边的参数是常量
};
* 常量成员函数的含义(成员函数 + const)
*
* 在类的 非 常量成员函数中,this指针类型是 X *const(指
* 针常量),而在 常量成员函数中,this指针类型是const X *const
* 不能被修改,这就是常量成员函数和非常量成员函数的区别.
*************************************************/
//例1
class X
{
private:
static int expensiveOperation();
int *buffer_;
bool isComputed_;
int computedValue_;
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_;
};
/*************************************************
* 如果我们需要在常量函数中修改类成员怎么办?
* 类的非静态变量数据成员可以声明为mutable,这将允许
* 它们的值可以被常量成员函数(当然也包括成员函数)所修改.
*************************************************/
//例2
class X
{
private:
static int expensiveOperation();
int *buffer_;
mutable bool isComputed_; //注意 mutable
mutable int computedValue_; //注意 mutable
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_;
};
/*************************************************
* 下面通过操作符的重载来深刻理解常量成员函数
*************************************************/
//例3
class X
{
public:
//...
int &operator [](int index); // ①
const int &operator [](int index) const; //②
//...
};
void main()
{
int i = 10;
X a;
a[5] = i; //重载 ①
const X b;
i = b[5]; //重载 ②
}
class X
{
public:
X operator + (const X &rightArg); //左边的参数是非常量
X operator + (const X &rightArg) const; //左边的参数是常量
};
- c++常量成员函数详解
- 详解成员函数的常量性
- C++常量详解(二)常量形参,常量返回值,常量成员函数
- 常量成员函数
- 常量成员函数
- const常量成员函数
- 常量成员函数
- 成员函数常量
- 常量成员函数
- 常量成员函数(const)
- 常量成员函数
- c++常量成员函数
- 常量成员函数
- C++-必知必会_常量成员函数的含义(条款10)
- C++常量成员常量返回值详解
- C++const对象 常量数据 常量成员函数 const指针 const引用
- 常量对象与常量成员函数
- 常量对象与常量成员函数
- 由笔试题引发的知识点
- Oracle中常用字段类型
- android 弹出视频
- Extjs控件学习(一)--Ext.Component
- 如何取得文件的超大图标 256*256
- c++常量成员函数详解
- Android开发之:Adapter的继承结构
- ARCH wait on SENDREQ
- Android开发之手机铃声代码实现
- 浏览器开发工具的25个秘密
- Windows CE 电源管理
- Android开发之SQLite技术详解
- 资治通鉴,卷一百三十四,沈约论刘宋灭国、佞臣惑主,刘宋禅位萧齐
- Overriding and Hiding Methods