为什么我们不能在 const 对象上调用函数?
why can't we call a function on a const object?
问题出在 main
,我想在 display()
函数上调用 set()
函数,return 是对 const 的引用。
#include "stdafx.h"
#include "iostream"
#include "string"
using std::cin;
using std::cout;
using std::endl;
using std::string;
using std::ostream;
class screen
{
public:
typedef string::size_type pos;
screen() = default;
screen(pos ht, pos wd, char c) :height(ht), width(ht),contents(ht*wd, c){}
screen(pos ht, pos wd) :height(ht), width(wd), contents(ht*wd, ' '){}
char get()const{ return contents[cursor]; }
inline char get(pos r, pos c)const;
screen &move(pos r, pos c);
screen &set(char);
screen &set(pos r, pos c,char);
const screen& display(ostream &os)const;
pos cursor;
pos height, width;
string contents;
};
const screen& screen::display(ostream &os)const
{
os<< contents;
return *this;
}
char screen::get(pos r, pos c)const
{
pos row = r*width;
return contents[row + c];
}
screen& screen::move(pos r, pos c)
{
pos row = r*width;
cursor = row + c;
return *this;
}
screen& screen::set(char c)
{
contents[cursor] = c;
return *this;
}
screen& screen::set(pos r, pos c, char ch)
{
pos row = r*width;
contents[row + c] = ch;
return *this;
}
int main()
{
screen myscreen(50, 50,'0');
myscreen.display(cout).set('#');//the problem is here!
return 0;
}
我只想知道为什么 set('#')
不能在 display(cout)
上调用,return 是对 const 的引用。
可以 set('#').display(cout)
但不能 display(cout).set('#')
abject 的常量如何对调用产生这样的影响?
我建议阅读更多有关 const
在 C++ 中的具体含义的信息。 const
的总体目的是防止对象被修改,因此由于非 const
方法有可能修改对象,因此不能在 const
对象上调用它们或const
对对象的引用。
如果调用成员函数,则有一个隐式 this
指针作为参数传递给该函数。在你的例子中,this
指针是 const
,它不能在你的 set
函数中被修改。所以失败了。
问题出在 main
,我想在 display()
函数上调用 set()
函数,return 是对 const 的引用。
#include "stdafx.h"
#include "iostream"
#include "string"
using std::cin;
using std::cout;
using std::endl;
using std::string;
using std::ostream;
class screen
{
public:
typedef string::size_type pos;
screen() = default;
screen(pos ht, pos wd, char c) :height(ht), width(ht),contents(ht*wd, c){}
screen(pos ht, pos wd) :height(ht), width(wd), contents(ht*wd, ' '){}
char get()const{ return contents[cursor]; }
inline char get(pos r, pos c)const;
screen &move(pos r, pos c);
screen &set(char);
screen &set(pos r, pos c,char);
const screen& display(ostream &os)const;
pos cursor;
pos height, width;
string contents;
};
const screen& screen::display(ostream &os)const
{
os<< contents;
return *this;
}
char screen::get(pos r, pos c)const
{
pos row = r*width;
return contents[row + c];
}
screen& screen::move(pos r, pos c)
{
pos row = r*width;
cursor = row + c;
return *this;
}
screen& screen::set(char c)
{
contents[cursor] = c;
return *this;
}
screen& screen::set(pos r, pos c, char ch)
{
pos row = r*width;
contents[row + c] = ch;
return *this;
}
int main()
{
screen myscreen(50, 50,'0');
myscreen.display(cout).set('#');//the problem is here!
return 0;
}
我只想知道为什么 set('#')
不能在 display(cout)
上调用,return 是对 const 的引用。
可以 set('#').display(cout)
但不能 display(cout).set('#')
abject 的常量如何对调用产生这样的影响?
我建议阅读更多有关 const
在 C++ 中的具体含义的信息。 const
的总体目的是防止对象被修改,因此由于非 const
方法有可能修改对象,因此不能在 const
对象上调用它们或const
对对象的引用。
如果调用成员函数,则有一个隐式 this
指针作为参数传递给该函数。在你的例子中,this
指针是 const
,它不能在你的 set
函数中被修改。所以失败了。