返回信息流#include <iostream>
using namespace std;
class A
{
public:
int m;
public:
A(int n)
{
m = n;
}
};
A foo()
{
A a(2);
return a;
}
int main()
{
A *p = NULL;
p = &(foo());//p指向的是一个临时对象吗,临时对象不是马上就会析构吗?下面怎么还能访问?
cout << p->m << endl;
A &r = foo();
r.m = 3;
cout << r.m << endl;
system("pause");
return 0;
}
如注释所述。。求问!!!
这是一条镜像帖。来源:北邮人论坛 / cpp / #81496同步于 2014/8/12
该镜像源已超过 30 天没有更新,可能在源站已被删除。
CPP机器人发帖
[问题]请问,关于临时变量(非tmp局部变量)
westorb
2014/8/12镜像同步15 回复
订阅后,新回复会通过你的通知中心匿名送达。
9 条回复
参考4楼的吧,如果返回一个临时对象最好是用const 修饰,因为临时对象默认是常量,还有对p进行赋值的过程 是先生成临时对象,然后拷贝出一份相同的内存到函数体外,最后用p 指向 那块内存。所以后面是可以访问的。
查了下C++11标准:
12.2 Temporary objects [class.temporary]
5 The second context is when a reference is bound to a temporary. The temporary to which the reference is
bound or the temporary that is the complete object of a subobject to which the reference is bound persists
for the lifetime of the reference except:
— A temporary bound to a reference member in a constructor’s ctor-initializer (12.6.2) persists until the
constructor exits.
— A temporary bound to a reference parameter in a function call (5.2.2) persists until the completion of
the full-expression containing the call.
— The lifetime of a temporary bound to the returned value in a function return statement (6.6.3) is not
extended; the temporary is destroyed at the end of the full-expression in the return statement.
— A temporary bound to a reference in a new-initializer (5.3.4) persists until the completion of the
full-expression containing the new-initializer. [Example:
struct S { int mi; const std::pair<int,int>& mp; };
S a { 1, {2,3} };
S* p = new S{ 1, {2,3} }; // Creates dangling reference
— end example ] [ Note: This may introduce a dangling reference, and implementations are encouraged
to issue a warning in such a case. — end note ]
The destruction of a temporary whose lifetime is not extended by being bound to a reference is sequenced
before the destruction of every temporary which is constructed earlier in the same full-expression. If the
lifetime of two or more temporaries to which references are bound ends at the same point, these temporaries
are destroyed at that point in the reverse order of the completion of their construction. In addition, the
destruction of temporaries bound to references shall take into account the ordering of destruction of objects
with static, thread, or automatic storage duration (3.7.1, 3.7.2, 3.7.3); that is, if obj1 is an object with the
same storage duration as the temporary and created before the temporary is created the temporary shall be
destroyed before obj1 is destroyed; if obj2 is an object with the same storage duration as the temporary and
created after the temporary is created the temporary shall be destroyed after obj2 is destroyed. [Example:
struct S {
S();
S(int);
friend S operator+(const S&, const S&);
~S();
};
S obj1;
const S& cr = S(16)+S(23);
S obj2;
the expression S(16) + S(23) creates three temporaries: a first temporary T1 to hold the result of the
expression S(16), a second temporary T2 to hold the result of the expression S(23), and a third temporary
T3 to hold the result of the addition of these two expressions. The temporary T3 is then bound to the reference
cr. It is unspecified whether T1 or T2 is created first. On an implementation where T1 is created before
T2, it is guaranteed that T2 is destroyed before T1. The temporaries T1 and T2 are bound to the reference
parameters of operator+; these temporaries are destroyed at the end of the full-expression containing the
call to operator+. The temporary T3 bound to the reference cr is destroyed at the end of cr’s lifetime,
that is, at the end of the program. In addition, the order in which T3 is destroyed takes into account the
destruction order of other objects with static storage duration. That is, because obj1 is constructed before
T3, and T3 is constructed before obj2, it is guaranteed that obj2 is destroyed before T3, and that T3 is
destroyed before obj1. — end example ]
【 在 westorb 的大作中提到: 】
: #include <iostream>
: using namespace std;
: class A
: ...................