Foo* foo1 = new Foo ();
Creates an object of type Foo in dynamic memory. foo1 points to this. Generally, you will not use raw pointers in C ++, but rather a smart pointer. If Foo was a POD type, this would initialize the value (it does not apply here).
Foo* foo2 = new Foo;
Identical before, since Foo not a POD type.
Foo foo3;
Creates a Foo object named foo3 in automatic storage.
Foo foo4 = Foo::Foo();
Uses copy-initialization to create a Foo object named foo4 in automatic storage.
Bar* bar1 = new Bar ( *new Foo() );
Uses the Bar transform constructor to create an object of type Bar in dynamic storage. bar1 is a pointer to it.
Bar* bar2 = new Bar ( *new Foo );
Same as before.
Bar* bar3 = new Bar ( Foo foo5 );
This is simply not valid syntax. You cannot declare a variable there.
Bar* bar3 = new Bar ( Foo::Foo() );
Will work and work on the same principle with 5 and 6, if bar3 not declared in 7.
5 and 6 contain memory leaks.
Syntax like new Bar ( Foo::Foo() ); not ordinary. Usually this is new Bar ( (Foo()) ); - additional bracket for the most unpleasant analysis. (corrected)
Luchian Grigore 03 Sep 2018-12-12T00: 00Z
source share