由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - C++ question
相关主题
为啥 const Base cb 要求Base() {} 而 const vBase vb 不呢?c++问题,请高人指点迷津,c++ faq lite的一个例子
question about const reference关于C/C++里的Static variable的memory allocation/initializa
c++ initialize struct问个copy constructor的问题
const object一道c++的考古题
C++ (direct vs indirect initialization)问个 ctor/copy ctor的问题
如何 initialize array member?请教c++ interface class问题
问一个 copy constructor 的问题 (C++)C++11痛并快乐着
Re: VC里面的stl支持是不是很弱?template 类的继承问题
相关话题的讨论汇总
话题: c++话题: const话题: reference话题: object话题: foo
进入Programming版参与讨论
1 (共1页)
T******r
发帖数: 257
1
class B {};
B *p = &B();
B &r = B();
有啥问题?
f*******y
发帖数: 988
2
要new一下
否则B()是临时的
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

相关主题
如何 initialize array member?c++问题,请高人指点迷津,c++ faq lite的一个例子
问一个 copy constructor 的问题 (C++)关于C/C++里的Static variable的memory allocation/initializa
Re: VC里面的stl支持是不是很弱?问个copy constructor的问题
进入Programming版参与讨论
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是定义的最混蛋也最混乱的地方,每次遇到这个都得先查一下书。
1 (共1页)
进入Programming版参与讨论
相关主题
template 类的继承问题C++ (direct vs indirect initialization)
急问:compile and build dependency如何 initialize array member?
C++ question问一个 copy constructor 的问题 (C++)
C++ 问题Re: VC里面的stl支持是不是很弱?
为啥 const Base cb 要求Base() {} 而 const vBase vb 不呢?c++问题,请高人指点迷津,c++ faq lite的一个例子
question about const reference关于C/C++里的Static variable的memory allocation/initializa
c++ initialize struct问个copy constructor的问题
const object一道c++的考古题
相关话题的讨论汇总
话题: c++话题: const话题: reference话题: object话题: foo