mutable 是不是如下这么用?会出错

unhappyless_14 2006-12-06 10:20:49
#include<iostream>
using namespace std;
class testmutable
{
public:
testmutable(int i=0):num(i){}
void say()
{
cout<<"const function"<<num<<endl;
}

private:
mutable int num; // 用在这
};

int main()
{
const testmutable one;
one.say();
}
...全文
225 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
missilery 2006-12-06
  • 打赏
  • 举报
回复
http://book.devworld.cn/book/download.asp?softid=228&downid=0&id=545
去下载一<C++ Primer中文版>电子书
北极猩猩 2006-12-06
  • 打赏
  • 举报
回复
呵呵,来晚了。

补充一点
通常mutable的成员都是不会影响对象的状态的数据,换句话说,只有有它没它不影响类的外部功能,这个成员才是合作mutable
OOPhaisky 2006-12-06
  • 打赏
  • 举报
回复
还要提醒楼主,不可以用const和mutable同时修饰一个data member:
class testmutable
{
public:
//......

private:
const mutable int num; //error:const和mutable不可以同时用
};
OOPhaisky 2006-12-06
  • 打赏
  • 举报
回复
mutable的作用是:让一个数据成员可以在const成员函数中被修改。
class testmutable
{
public:
testmutable(int i=0):num(i){}
void say()const//注意这里的const,说明此成员函数是const的
{
num = 3;//由于num是const的,所以可以在此const成员函数中进行修改
}

private:
mutable int num;
};
missilery 2006-12-06
  • 打赏
  • 举报
回复
Mutable Data Members
It sometimes (but not very often) happens that a class has a data member that we want to be able to modify, even inside a const member function. We can indicate such members by declaring them as mutable.

A mutable data member is a member that is never const, even when it is a member of a const object. Accordingly, a const member function may change a mutable member. To declare a data member as mutable, the keyword mutable must precede the declaration of the member:

class Screen {
public:
// interface member functions
private:
mutable size_t access_ctr; // may change in a const members
// other data members as before
};



We've given Screen a new data member named access_ctr that is mutable. We'll use access_ctr to track how often Screen member functions are called:

void Screen::do_display(std::ostream& os) const
{
++access_ctr; // keep count of calls to any member function
os << contents;
}



Even though do_display is const, it can increment access_ctr. That member is a mutable member, so any member function, including const functions, can change the value of access_ctr.
lann64 2006-12-06
  • 打赏
  • 举报
回复
#include<iostream>
using namespace std;
class testmutable
{
public:
testmutable(int i=0):num(i){}
void say() const
{
num++; //一样可以修改num的值。
cout<<"const function: "<<num<<endl;
}

private:
mutable int num; // 用在这
};

int main()
{
const testmutable one;
one.say();
}
这样用。
你的say函数忘了用const,对于const对象当然编译通不过。
v2002750 2006-12-06
  • 打赏
  • 举报
回复
#include<iostream>
using namespace std;
class testmutable
{
public:
testmutable(int i=0):num(i){}

void say()
{
cout<<"const function"<<num<<endl;
}

public:
mutable int num; // 用在这
};

void main()
{
const testmutable one;
one.num=5;
}
v2002750 2006-12-06
  • 打赏
  • 举报
回复
一个const对象能调用一个非const的方法吗?
taodm 2006-12-06
  • 打赏
  • 举报
回复
出啥错?
另外,楼主,你在用啥书学C++?
找c++ Primer,看书后索引表,查mutable

64,654

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧