Jump to content
  • Advertisement
Sign in to follow this  
Tispe

Uses of 'new' syntax.

This topic is 2017 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, what is the difference between these two ?

 

 MyClass* p(new MyClass);

 

MyClass* p = new MyClass;

 

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

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

Edited by rnlf

Share this post


Link to post
Share on other sites

... 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.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!