return (const std::string &) *this 请问这句是什么意思??

GAOQUAN2 2008-09-09 05:18:07
// winei.h

class Wine: private std::string
{
private:
........
........

public:
const std::string & Label()const {return (const std::string &) *this;} // ??
.......
........
}

// winei.cpp

void Wine::Show() const
{
cout << "Wine: " << Label() << endl;

}


return (const std::string &) *this 这句是什么意思?? 小弟不太明白 this不是用来指向wine类 成员和方法的吗?
...全文
192 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
keyayi 2008-09-09
  • 打赏
  • 举报
回复
const std::string & 是返回类型

Label()const 函数名后加const,表示这函数是常成员函数,这样做的好处是,让使用者一目了然地知道该成员函数不会改变对象值

return (const std::string &) *this //返回指向成基类(也就是string)的常引用

讲的不好,多多指教
qq668161 2008-09-09
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 xkyx_cn 的回复:]
把*this(调用Label()的对象)的类型强制转换为const std::string &,然后返回
[/Quote]
LZ你可以结帖了
wangdeqie 2008-09-09
  • 打赏
  • 举报
回复

return (const std::string &) *this //返回指向成基类(也就是string)的常引用
xkyx_cn 2008-09-09
  • 打赏
  • 举报
回复
把*this(调用Label()的对象)的类型强制转换为const std::string &,然后返回
bitxinhai 2008-09-09
  • 打赏
  • 举报
回复
返回this指针指向对象的引用!!!!
星羽 2008-09-09
  • 打赏
  • 举报
回复
wine是用string派生的啊

64,654

社区成员

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

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