如果临时匿名对象自动是一个常对象,那么让函数返回一个常对象或者是非常对象还有何区别?

Andy_y 2009-01-24 10:12:12
就如标题所说,引用thinking in C++中的原话:
because the temporary object is automatically const, so whether you explicitly make the return value const or not has no effect!!!
疑问就在此:
const T f();和T g();还有何区别???
f().非常函数()和g().非常函数()都无法通过编译,因为临时匿名对象是一个常对象,而无法利用常对象调用非常函数。



...全文
230 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
nineforever 2009-02-01
  • 打赏
  • 举报
回复
并没有所谓临时对象“自动”const一说。
临时对象是右值,只能绑定到常引用。但是右值可以调用它的非const成员函数。相反,常量不能调用它的非const成员函数
jstar920 2009-01-25
  • 打赏
  • 举报
回复
const T f1()没什么意义
const T& f1()这样才有意义
hityct1 2009-01-25
  • 打赏
  • 举报
回复
不懂。关注。
xiaoyisnail 2009-01-24
  • 打赏
  • 举报
回复
楼主是不是断章取义了?
waizqfor 2009-01-24
  • 打赏
  • 举报
回复
UP
taodm 2009-01-24
  • 打赏
  • 举报
回复
如果《thinking in C++》说错了呢。
chin_chen 2009-01-24
  • 打赏
  • 举报
回复
class ret
{
public:
void func()const
{ }
};

ret f()
{
ret value;
return value;
}

const ret g()
{
ret value;
return value;
}

int main(int argc, char** argv)
{
f().func(); // passed
g().func(); // passed

return 1;
}

楼上的因为是const对象,那么它只能用const方法了。
BenjaminHuang 2009-01-24
  • 打赏
  • 举报
回复
class ret
{
public:
void func() { }
};

ret f()
{
ret value;
return value;
}

const ret g()
{
ret value;
return value;
}

int main(int argc, char** argv)
{
f().func(); // passed
g().func(); // error: passing `const ret' as `this' argument of `void ret::func()' discards qualifiers

return 1;
}

测试环境: Linux/g++
这里是否和各位预期的不一样?
wjb_yd 2009-01-24
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 taodm 的回复:]
如果《thinking in C++》说错了呢。
[/Quote]
那本书的那一节讲的确实有些自相矛盾
Andy_y 2009-01-24
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 chin_chen 的回复:]
引用楼主 Andy_y 的帖子:

because the temporary object is automatically const, so whether you explicitly make the return value const or not has no effect!!


这句话是说,由于临时对象是自动为const的,如果函数返回值是临时对象的,那么你加不加const都一样,它们都是const的。
[/Quote]对,你说得对!就是因为加不加const都一样,它们都是const的,那么const T f();和T g();还有何区别???
chin_chen 2009-01-24
  • 打赏
  • 举报
回复
[Quote=引用楼主 Andy_y 的帖子:]

because   the   temporary   object   is   automatically   const,   so   whether   you   explicitly   make   the   return   value   const   or   not   has   no   effect!!

[/Quote]
这句话是说,由于临时对象是自动为const的,如果函数返回值是临时对象的,那么你加不加const都一样,它们都是const的。
Andy_y 2009-01-24
  • 打赏
  • 举报
回复
#include <iostream>
using namespace std;
//本程序通过G++编译检测,错误信息已给出

class T
{
public:
void g(){}
};

const T f1(){return T t;}
T f2(){return T t;}


int main()
{
//验证临时匿名对象是不是一个常对象只有一个方法:
//*******************利用临时匿名对象调用非常函数,若成功就可以证明临时匿名对象不会总是自动为一个常对象 **************

/*就目前个人理解程度,还没发现第二种方法,曾经尝试过
让临时匿名对象绑定到一个普通引用(非常引用),看看是否成功 !
但是不管临时匿名对象是不是常对象,临时匿名对象都无法绑定到普通引用
(因为修改一个临时匿名对象达不到函数的目的,本来让一个函数的参数为一个普通引用
,就是为了修改该对象,若修改的是另一个临时的对象,显然会发生逻辑错误,所以
标准C++不允许这种绑定),所证该方法尝试失败!
*/
//这里有点偏移论点了,下面回到正题



f1().g();//Wrong
//G++中的错误信息:passing `const T' as `this' argument of `void T::g()' discards qualifiers
//常对象无法调用非常函数,证明f1()返回的临时匿名对象是常对象,
//因为此对象是通过const T(t) 这样的拷贝构造函数产生的
f2().g();//OK
//普通对象可以调用非常函数 ,证明f2()返回的临时匿名对象不是常对象,
//因为此对象是通过 T(t) 这样的拷贝构造函数产生的


//结论:如果通过值返回的函数,是否声明为一个const对象,还是有区别的,所以thingking C++中的这段话(如下)还有待论证
//(标题)Return by value as const(影印版第二版的第507页)
/* Returning by value as a const can seem a bit subtle at first,
so it deserves a bit more explanation.Consider the binary operator+.
If you use it as a f(a+b)此处的a+b为加号操作符重载函数,返回一个常对象,
the result of a+b becomes a temporary object that is used in the call to f().
Because it's a temporary, it's automatically const, so whether you explicitly
make the return value const or not has no effect.
*/



return 0;
}

65,211

社区成员

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

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