Creating an instance of a class

What is the difference between lines 1, 2, 3, 4?

When do I use each?

Why does line 3 print constructor Foo , and line 7 returns an error, and line 8 does not work?

 #include <iostream> using namespace std; class Foo { public: Foo ( ) { cout << "constructor Foo\n"; } }; class Bar { public: Bar ( Foo ) { cout << "constructor Bar\n"; } }; int main() { /* 1 */ Foo* foo1 = new Foo (); /* 2 */ Foo* foo2 = new Foo; /* 3 */ Foo foo3; /* 4 */ Foo foo4 = Foo::Foo(); /* 5 */ Bar* bar1 = new Bar ( *new Foo() ); /* 6 */ Bar* bar2 = new Bar ( *new Foo ); /* 7 */ Bar* bar3 = new Bar ( Foo foo5 ); /* 8 */ Bar* bar3 = new Bar ( Foo::Foo() ); return 1; } 
+62
c ++ constructor class
Sep 03 '12 at 13:18
source share
3 answers
  /* 1 */ 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).

  /* 2 */ Foo* foo2 = new Foo; 

Identical before, since Foo not a POD type.

  /* 3 */ Foo foo3; 

Creates a Foo object named foo3 in automatic storage.

  /* 4 */ Foo foo4 = Foo::Foo(); 

Uses copy-initialization to create a Foo object named foo4 in automatic storage.

  /* 5 */ 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.

  /* 6 */ Bar* bar2 = new Bar ( *new Foo ); 

Same as before.

  /* 7 */ Bar* bar3 = new Bar ( Foo foo5 ); 

This is simply not valid syntax. You cannot declare a variable there.

  /* 8 */ 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)

+74
03 Sep
source share
  • Allocates some dynamic memory from free storage and creates an object in this memory using its default constructor. You never delete it, so a memory leak occurs.
  • Just like 1; for custom types, parentheses are optional.
  • Allocates some automatic memory and creates an object in this memory using its default constructor. Memory is freed automatically when an object goes out of scope.
  • Similarly 3. In theory, the named object foo4 initialized by building by default, copying and destroying the temporary object; this is usually eliminated, giving the same result as 3.
  • Selects a dynamic object, then initializes the second, copying the first. Both objects leaked; and there is no way to delete the first, since you did not point to it.
  • Just like 5.
  • Not compiled. Foo foo5 is an declaration, not an expression; function (and constructor) arguments must be expressions.
  • Creates a temporary object and initializes the dynamic object by copying it. Only a dynamic object flows; the temporary is automatically destroyed at the end of the full expression. Note that you can only create a temporary value with Foo() , not the equivalent of Foo::Foo() (or indeed Foo::Foo::Foo::Foo::Foo() )

When do I use each?

  • Do not if you do not like the unnecessary decoration of your code.
  • If you want to create an object that selects the current area. Remember to delete it when you are done with it, and learn how to use smart pointers to better manage your life.
  • If you need an object that exists only in the current area.
  • Do not think if you think that 3 looks boring and that add unnecessary decoration.
  • Do not do this because it leaks memory without a chance of recovery.
  • Do not do this because it leaks memory without a chance of recovery.
  • No need because it will not compile
  • If you want to create a dynamic Bar from a temporary Foo .
+16
Sep 03
source share

Lines 1,2,3,4 will call the default constructor. They are essentially different, since 1,2 is a dynamically created object, and 3,4 are statically created objects.

On line 7, you create an object inside the argument call. So this is a mistake.

And lines 5 and 6 are an invitation to a memory leak.

+4
Sep 03 '12 at 13:25
source share



All Articles