评论区的大哥的解释很有道理,在下面贴出来,我收回之前的言论:
class::func( ) const
成员函数的返回值是string&
不是const string&
,这是问题所在。
class::func( ) const
内的一切类成员都是const
,this
指针是const*
,ps类型是const string*
,return
句*ps
类型是const string
返回为string&
,有些编译器会警告报错。请严格控制返回值类型是否const
,非const_cast<>
不解const
==================== 以上2022.11.5更新
我自定义了一个类 HasPtr
,并创建了一个它的常量对象,get_str
返回成员的引用。
#include <iostream>
#include "HasPtr_ex30test.h"
using std::cout;
int main()
{
const HasPtr hp(0, "hello");
cout << hp.get_str() << '\n';
hp.get_str() = "jesus";
cout << hp.get_str() << '\n';
return 0;
}
输出:
hello
jesus
看到这一幕真的很震惊,const
对象的成员竟然被修改了。这究竟怎么回事,下面给出头文件。
#ifndef HASPTR_EX30TEST_H
#define HASPTR_EX30TEST_H
#include <string>
using std::string;
class HasPtr
{
friend void swap(HasPtr &, HasPtr &);
public:
HasPtr(int i_, const string &s = "") : ps(new string(s)), i(i_) {}
HasPtr(const HasPtr &hp) : ps(new string(*hp.ps)), i(hp.i) {}
HasPtr &operator=(const HasPtr &hp);
~HasPtr() { delete ps; }
string &get_str() const
{
return *ps;
}
private:
string *ps;
int i;
};
// 其余函数
#endif
关键就在 get_str()
成员函数上,如果 ps
是个 string
对象,肯定是不能修改的了,但偏偏它是一个 string
指针。这会发生什么呢?
return *ps;
相当于 return *((*this).ps)
。我们知道这是一个常量成员函数,所以 *this
是常量,所以 (*this).ps
也是常量,也是一个 string
指针。问题来了,这个常量是顶层还是底层?答案是顶层。如果不信,我们做如下改动:
string &get_str() const
{
*ps = "ggg";
return *ps;
}
main
函数不变,输出:
ggg
ggg
可以看到在常量成员函数中成功修改了 *ps
的值,是不是不可思议。其实不能修改的是 ps
,但 *ps
确实可以修改,这也印证了 ps
就是顶层 const
。
*((*this).ps)
是对顶层 const
解引用,当然也就丢弃了常量特性了。所以非常量可以绑定到 string &
上面,进而可以在 main
函数中修改为 "jesus"
。