Whenever you run a program, it's an instance of that program. In languages that create objects from classes, an object is an instantiation of a class. That is, an object is a member of a given class with specified values rather than variables.
When you use the keyword new for example JFrame j = new JFrame(); you are creating an instance of the class JFrame . The new operator instantiates a class by allocating memory for a new object and returning a reference to that memory.
Solution. The process of creating an object is called Instantiation.
/* 1 */ Foo* foo1 = new Foo ();
Creates an object of type Foo
in dynamic memory. foo1
points to it. Normally, you wouldn't use raw pointers in C++, but rather a smart pointer. If Foo
was a POD-type, this would perform value-initialization (it doesn't apply here).
/* 2 */ Foo* foo2 = new Foo;
Identical to before, because Foo
is not a POD type.
/* 3 */ Foo foo3;
Creates a Foo
object called foo3
in automatic storage.
/* 4 */ Foo foo4 = Foo::Foo();
Uses copy-initialization to create a Foo
object called foo4
in automatic storage.
/* 5 */ Bar* bar1 = new Bar ( *new Foo() );
Uses Bar
's conversion constructor to create an object of type Bar
in dynamic storage. bar1
is a pointer to it.
/* 6 */ Bar* bar2 = new Bar ( *new Foo );
Same as before.
/* 7 */ Bar* bar3 = new Bar ( Foo foo5 );
This is just invalid syntax. You can't declare a variable there.
/* 8 */ Bar* bar3 = new Bar ( Foo::Foo() );
Would work and work by the same principle to 5 and 6 if bar3
wasn't declared on in 7.
5 & 6 contain memory leaks.
Syntax like new Bar ( Foo::Foo() );
is not usual. It's usually new Bar ( (Foo()) );
- extra parenthesis account for most-vexing parse. (corrected)
foo4
is initialised by default-constructing, copying and destroying a temporary object; usually, this is elided giving the same result as 3.Foo foo5
is a declaration, not an expression; function (and constructor) arguments must be expressions.Foo()
rather than the equivalent Foo::Foo()
(or indeed Foo::Foo::Foo::Foo::Foo()
)When do I use each?
Bar
from a temporary Foo
.Lines 1,2,3,4 will call the default constructor. They are different in the essence as 1,2 are dynamically created object and 3,4 are statically created objects.
In Line 7, you create an object inside the argument call. So its an error.
And Lines 5 and 6 are invitation for memory leak.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With