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;
|
|
|
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?
|