T******r 发帖数: 257 | 1 class B {};
B *p = &B();
B &r = B();
有啥问题? |
f*******y 发帖数: 988 | |
t****t 发帖数: 6806 | 3
~~~~~~~~~~~~~~~
the temporary object B() is lost after the expression. therefore its address
is invalid.
~~~~~~~~~~~~~
in this case, the temporary object B() continue to exist with r. however,
you MUST bind to a const, non-volatile reference (const B& r=B();),
otherwise it's not correct.
【在 T******r 的大作中提到】 : class B {}; : B *p = &B(); : B &r = B(); : 有啥问题?
|
p*u 发帖数: 2454 | 4 neither can pass compilation with a up-to-date compiler.
~~~~~~taking temporary memory address
~~~~~~invalid initialization
【在 T******r 的大作中提到】 : class B {}; : B *p = &B(); : B &r = B(); : 有啥问题?
|
f*******y 发帖数: 988 | 5
address
还是老大强啊,const B& 居然真的可以
【在 t****t 的大作中提到】 : : ~~~~~~~~~~~~~~~ : the temporary object B() is lost after the expression. therefore its address : is invalid. : ~~~~~~~~~~~~~ : in this case, the temporary object B() continue to exist with r. however, : you MUST bind to a const, non-volatile reference (const B& r=B();), : otherwise it's not correct.
|
t****t 发帖数: 6806 | 6 干嘛,我没peng你就很好了
【在 p*u 的大作中提到】 : neither can pass compilation with a up-to-date compiler. : : ~~~~~~taking temporary memory address : ~~~~~~invalid initialization
|
T******r 发帖数: 257 | 7 晕啊.MS VC++ 2005 express不但能编译还能运行.
address
【在 t****t 的大作中提到】 : 干嘛,我没peng你就很好了
|
p*u 发帖数: 2454 | 8 enjoy dangling pointer and reference then.
【在 T******r 的大作中提到】 : 晕啊.MS VC++ 2005 express不但能编译还能运行. : : address
|
N*********y 发帖数: 105 | 9 靠,註意語言文明
【在 p*u 的大作中提到】 : enjoy dangling pointer and reference then.
|
y*h 发帖数: 107 | 10 microsoft垃圾都能运行, 现在每次都要在UNIX底下跑一边才放心
【在 T******r 的大作中提到】 : 晕啊.MS VC++ 2005 express不但能编译还能运行. : : address
|
|
|
p*u 发帖数: 2454 | 11 靠,注意使用简体汉字。
【在 N*********y 的大作中提到】 : 靠,註意語言文明
|
t****t 发帖数: 6806 | 12 第一个总是能编译运行的.
第二个应该是语法错误了.
【在 T******r 的大作中提到】 : 晕啊.MS VC++ 2005 express不但能编译还能运行. : : address
|
p****s 发帖数: 32405 | 13 应该用*p = new B() ?
【在 t****t 的大作中提到】 : 第一个总是能编译运行的. : 第二个应该是语法错误了.
|
y*h 发帖数: 107 | 14 这种用法有人用吗? 还是又是面世?
【在 T******r 的大作中提到】 : class B {}; : B *p = &B(); : B &r = B(); : 有啥问题?
|
p*u 发帖数: 2454 | 15 if default ctor has no parameters, just do a B* p = new B;
【在 p****s 的大作中提到】 : 应该用*p = new B() ?
|
N*********y 发帖数: 105 | 16 靠,就是要用正體中國漢字
【在 p*u 的大作中提到】 : 靠,注意使用简体汉字。
|
t****t 发帖数: 6806 | 17 hehe, be careful when you write things...it's either
p=new B();
or
B * p=new B();
【在 p****s 的大作中提到】 : 应该用*p = new B() ?
|
q*****g 发帖数: 72 | 18
address
why must bind to a const reference?
【在 t****t 的大作中提到】 : hehe, be careful when you write things...it's either : p=new B(); : or : B * p=new B();
|
t****t 发帖数: 6806 | 19 it happens that these temp objects are rvalues, so it's const.
it's not often to write like cosnt B& rb=B(); but it's very normal in
function calls, like
void foo(const B& b);
int main()
{
foo(B());
}
if you declare foo as void foo(B&), the call will fail.
【在 q*****g 的大作中提到】 : : address : why must bind to a const reference?
|
x*p 发帖数: 66 | 20 C++ 里面这个const是定义的最混蛋也最混乱的地方,每次遇到这个都得先查一下书。 |
L*********r 发帖数: 92 | 21 In VC++,
intialize a const reference with temp object is the right way.
intialize a reference with temp object is a warning not an error.
【在 t****t 的大作中提到】 : it happens that these temp objects are rvalues, so it's const. : it's not often to write like cosnt B& rb=B(); but it's very normal in : function calls, like : void foo(const B& b); : int main() : { : foo(B()); : } : if you declare foo as void foo(B&), the call will fail.
|
t****t 发帖数: 6806 | 22 还好吧.一共也没几条规则,都很intuitive的.
【在 x*p 的大作中提到】 : C++ 里面这个const是定义的最混蛋也最混乱的地方,每次遇到这个都得先查一下书。
|