fought these days.
The problem is calling the constructor.
I wrote a piece of code, for example:
#include <iostream> using namespace std; class Foo { private: int _n; public: Foo() { Foo(5);} Foo(int n) {_n=n; cout << n << endl; } }; int main() { Foo* foo = new Foo(); return 0; }
When I built the Foo object outside using the default constructor:
Foo* f = new Foo();
I suppose the variable _n is 5, but it is NOT.
This is normal in Java, but NOT in C ++.
Also in Visual C ++ 6 sp 6,
Foo() {this->Foo(5);}
work.
However, this expression rejects gcc / g ++ 4.
Finally, I found a solution.
A simple change to the default constructor in
Foo() {Foo(5);}
in
Foo() { new (this) Foo(5); }
solves the problem.
What does "this" do in parentheses?
source share