How to call a constructor in another constructor in c++
===

 was struggling these days.

The problem is the constructor calling.

I wrote a piece of code like:

    class Foo() {
    
      private: int _n;
    
      public:
    
      Foo() { Foo(5);}
    
      Foo(int n) {_n=n;}
    
    }

When I constructed a Foo object outside using the default constructor:

    Foo* f = new Foo();

I suppose variable _n is 5, however, it's NOT.

 

It's ok in Java but NOT in c++.

 

In addition, in Visual C++ 6 sp 6,

    Foo() {this->Foo(5);}

works.

However, this expression is refused by gcc/g++ 4.

 

Finally, I found out the solution.

Simply changing the default constructor into

    Foo() {Foo(5);}

into

    Foo() { new (this) Foo(5); }

solves the problem.

What the hell does "this" in paranthesis do?????

 

Do you guys know the explanation?