Uses of 'new' syntax.

Started by
7 comments, last by King Mir 10 years, 10 months ago

Hi, what is the difference between these two ?


 MyClass* p(new MyClass);


MyClass* p = new MyClass;

Advertisement

Hi, as far as I can see, compiler generates absolutely the same assembler code for both. So I guess its just a matter of typing preference.

This isn't a difference with 'new' at all - it's being used the same way in both examples. The difference is how the variable 'p' is being initialized. In the first example, the new object is passed to p's constructor, and in the second it's passed to p's assignment operator.

Seeing that p is a primitive type, there is no difference. If p was an instance of a class that actually had constructors and/or an assignment operator, then there would be a difference.

From the C++11 standard, section 8.5:

The form of initialization (using parentheses or =) is generally insignificant, but does matter when the initializer or the entity being initialized has a class type; see below.

So it should really result in the same assembly being created for pointer initialization. It has, by the way, little to do with the new keyword but rather with the initialization of the object p.

Edit: Damn you Hodgman. You're just to fast for me angry.png

... If p was an instance of a class that actually had constructors and/or an assignment operator, then there would be a difference.

Wouldn't it use the copyconstructor either way? At least in this case, with an assignment right after declaration.

BloodyEpi: According to section 12.6 of the standard:


Foo foo;
MyClass p(foo);

calls p's constructor which takes a foo while


Foo foo;
MyClass p = foo;

first creates p using the same constructor as in the previous example and then uses MyClass's copy or move constructor if available.

BloodyEpi: According to section 12.6 of the standard:


Foo foo;
MyClass p(foo);

calls p's constructor which takes a foo while


Foo foo;
MyClass p = foo;

first creates p using the same constructor as in the previous example and then uses MyClass's copy or move constructor if available.

The standard makes room for optimizations in these cases, it's called Copy Elision.

BloodyEpi: According to section 12.6 of the standard:


Foo foo;
MyClass p(foo);

calls p's constructor which takes a foo while


Foo foo;
MyClass p = foo;

first creates p using the same constructor as in the previous example and then uses MyClass's copy or move constructor if available.

The standard makes room for optimizations in these cases, it's called Copy Elision.

Copy elision still requires the corresponding constructors to be available. Copy elision does not eliminate the need for both a public conversion and copy constructor, even of only the conversion constructor is actually called.

In the second example, only the conversion constructor is called with copy elision, but if the copy constructor which isn't even called is private or otherwise not available in that context, the code is invalid and will fail to compile.

In C++11, the preferred way is to do this:

MyClass * p{new Myclass{}};

The idea being that there are subtle gotchas with the syntax (including the copy issue above), and the curly braces provide a uniform syntax.

This topic is closed to new replies.

Advertisement