<分区>
下面有一个类
class A
{
public:
string& getStr()
{
// Do a lot of work to get str
return str
}
const string& getStr() const;
};
我可以在第二个函数中调用第一个函数吗?我想这样做,因为第二个函数与第一个函数有很多共同的代码。不能这样:
const string& A::getStr() const
{
// Wrong
A temp;
return temp.getStr();
}
因为增加了一个新的temp,*this和temp之间的内部状态是不同的(*this != temp)。
可以像我描述的那样调用吗?