由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - what is the difference?
相关主题
问个copy constructor的问题请教一下,exception时,destructor一定会被调用么?
c++ questionconst object
一个 default constructor 的问题What does the default constructor do?
C++ Q01: private inheritance.c++ initialize struct
Is the order of initialization a, b, c or c, b, a?pthread_create inside a constructor
一道 memset in C++的题C++ 中 myobject * a =new myobject[n] 的问题
一个c++ constructor的问题, thanksTest your C++ knowledge...
抠字眼:assignment and initialize in C++请教几个C++问题
相关话题的讨论汇总
话题: widget话题: pod话题: what话题: difference话题: type
进入Programming版参与讨论
1 (共1页)
c*******9
发帖数: 6411
1
Widget* pw = new Widget;
Widget* pw = new Widget();
or the same?
d****p
发帖数: 685
2
for your case, no diff.
for case below, big diff
Widget a();
vs
Widget a;
z****e
发帖数: 2024
3
you can add one more:
(Widget) a();
and see if it is possible to have the following make sense:
(Widget a)();

【在 d****p 的大作中提到】
: for your case, no diff.
: for case below, big diff
: Widget a();
: vs
: Widget a;

r****o
发帖数: 1950
4
能不能具体说说这两个case有什么区别?

【在 d****p 的大作中提到】
: for your case, no diff.
: for case below, big diff
: Widget a();
: vs
: Widget a;

d****p
发帖数: 685
5
You are really good at confusing compiler :-)

【在 z****e 的大作中提到】
: you can add one more:
: (Widget) a();
: and see if it is possible to have the following make sense:
: (Widget a)();

d****p
发帖数: 685
6
The second one is a function declaration.

【在 r****o 的大作中提到】
: 能不能具体说说这两个case有什么区别?
r****o
发帖数: 1950
7
(Widget) a()应该和Widget a() 一样的吧。
(Widget a)() 如果合法的话,是个什么东西呢?

【在 z****e 的大作中提到】
: you can add one more:
: (Widget) a();
: and see if it is possible to have the following make sense:
: (Widget a)();

t****t
发帖数: 6806
8
you meant the first one...

【在 d****p 的大作中提到】
: The second one is a function declaration.
r****o
发帖数: 1950
9
He means the first one in the second case?

【在 t****t 的大作中提到】
: you meant the first one...
t****t
发帖数: 6806
10
i have to correct you about difference between
new Widget
and
new Widget()
although they are really really subtle.
if type Widget is non-POD type, these 2 expressions are the same.
if type Widget is POD type, the first form will leave the new object at
indeterminate state, while the second form will still do default-
initialization, which means all-zero init for POD type.
I agree this difference is completely artificial and mostly can not be
derived from other things.

【在 d****p 的大作中提到】
: for your case, no diff.
: for case below, big diff
: Widget a();
: vs
: Widget a;

相关主题
一道 memset in C++的题请教一下,exception时,destructor一定会被调用么?
一个c++ constructor的问题, thanksconst object
抠字眼:assignment and initialize in C++What does the default constructor do?
进入Programming版参与讨论
t****t
发帖数: 6806
11
Widget a(); // declare a function returning Widget
Widget a; // declare an object with type Widget

【在 r****o 的大作中提到】
: He means the first one in the second case?
t****t
发帖数: 6806
12
(Widget) a() is to call function a (declared somewhere else) and cast the
result to type Widget.

【在 r****o 的大作中提到】
: (Widget) a()应该和Widget a() 一样的吧。
: (Widget a)() 如果合法的话,是个什么东西呢?

r****o
发帖数: 1950
13
我看到Widget晕了,其实就是(int) a(),呵呵。

【在 t****t 的大作中提到】
: (Widget) a() is to call function a (declared somewhere else) and cast the
: result to type Widget.

d****p
发帖数: 685
14
You are right - 5.3.4 item 15 specifies this. Thanks for clarifying it out.
I actually just know this form (new T instead of new T(...)) recently. One
of my colleagues (the guy I mentioned
in my previous post with Java background) used this form. I initially felt
weird but later occasionally use it to
save typing ().
What I learned from this example is ALWAYS provide a constructor for even
POD class. It seems a bit
overkilling with simple struct but that's the cost as a C++ coder - a C
coder is

【在 t****t 的大作中提到】
: i have to correct you about difference between
: new Widget
: and
: new Widget()
: although they are really really subtle.
: if type Widget is non-POD type, these 2 expressions are the same.
: if type Widget is POD type, the first form will leave the new object at
: indeterminate state, while the second form will still do default-
: initialization, which means all-zero init for POD type.
: I agree this difference is completely artificial and mostly can not be

t****t
发帖数: 6806
15
what you said "always provide a constructor for POD class" is equivalent to
"do not use POD class" -- by definition, POD class can not have user-defined
constructors.
i think that's a little bit overkill...

【在 d****p 的大作中提到】
: You are right - 5.3.4 item 15 specifies this. Thanks for clarifying it out.
: I actually just know this form (new T instead of new T(...)) recently. One
: of my colleagues (the guy I mentioned
: in my previous post with Java background) used this form. I initially felt
: weird but later occasionally use it to
: save typing ().
: What I learned from this example is ALWAYS provide a constructor for even
: POD class. It seems a bit
: overkilling with simple struct but that's the cost as a C++ coder - a C
: coder is

d****p
发帖数: 685
16
Technically it is no longer a POD - practically it is a struct with an
explicit constructor.
Personally, I prefer the syntax of initializing a struct with constructor
instead of aggregate
MyStruct t = { ... } vs MyStruct t(...) - I think the second is clearer.
Or you guys love MyStruct t = MakeMyStruct(...)?

to
defined

【在 t****t 的大作中提到】
: what you said "always provide a constructor for POD class" is equivalent to
: "do not use POD class" -- by definition, POD class can not have user-defined
: constructors.
: i think that's a little bit overkill...

c*******9
发帖数: 6411
17
what is (Widget a)() ;
Is that a functor?
z****e
发帖数: 2024
18
unless use a macro, or i don't think the express will make sense.

【在 c*******9 的大作中提到】
: what is (Widget a)() ;
: Is that a functor?

1 (共1页)
进入Programming版参与讨论
相关主题
请教几个C++问题Is the order of initialization a, b, c or c, b, a?
[合集] 一些C++问题一道 memset in C++的题
请教问题一个c++ constructor的问题, thanks
find bugs of c++ codes抠字眼:assignment and initialize in C++
问个copy constructor的问题请教一下,exception时,destructor一定会被调用么?
c++ questionconst object
一个 default constructor 的问题What does the default constructor do?
C++ Q01: private inheritance.c++ initialize struct
相关话题的讨论汇总
话题: widget话题: pod话题: what话题: difference话题: type