Possible Duplicate:
C++ using this pointer in constructors
Like the title, may I do something like the following code?
class A;
class B {
public:
B(A* p);
...
};
class A {
B m;
public:
A():m(this){}
~A(){}
};
Possible Duplicate:
C++ using this pointer in constructors
Like the title, may I do something like the following code?
class A;
class B {
public:
B(A* p);
...
};
class A {
B m;
public:
A():m(this){}
~A(){}
};
Yes, you can passed a pointer to an object currently under construction. But you have to keep in mind, that the object isn't constructed completely yet. So basically what B can do in it's c'tor is store the pointer for later use.
An example where this is often used, is a std::stream and a stream buffer.