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(){}
};
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
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.