3

Possible Duplicate:
Shortcut for constructor

Are the following pieces of code the same in C++:

Piece1:

MyFunnyClass o = MyFunnyClass();

Piece2:

MyFunnyClass o;

I am aware that the following is not equivalent, but I am not sure about the two on the top:

MyFunnyClass o = MyFunnyClass () ;

MyFunnyClass o;
o = MyFunnyClass();
Community
  • 1
  • 1
BlueTrin
  • 9,610
  • 12
  • 49
  • 78

1 Answers1

2
MyFunnyClass o () ;

This does not define an object in any way at all. This is the Most Vexing Parse. o is a function which takes nothing and returns a MyFunnyClass, which you have declared.

The real syntax would be

MyFunnyClass o;

This would default-construct an object.

MyFunnyClass o = MyFunnyClass();

Value-constructs an object and then copies or moves it into o. Expect ellision here.

Puppy
  • 144,682
  • 38
  • 256
  • 465