I have two classes with a parent-child relationship (the Parent
class "has-a" Child
class), and the Child
class has a pointer back to the Parent
. It would be nice to initialize the parent pointer upon construction of the child, as follows:
class Child
{
public:
Child (Parent* parnet_ptr_) : parent_ptr(parent_ptr_) {};
private:
Parent* parent_ptr;
};
class Parent
{
public:
Parent() : child(this) {};
private:
Child child;
}
Now, I know people recommend not using this
in initialization list, and C++ FAQ says I'm gonna get a compiler warning (BTW, on VS2010, I don't get a warning), but I really like this better then calling some set function in Parent
's constructor. My questions are:
- Is the parent
this
pointer well-defined when theChild
object is being created? - If so, why is it considered bad practice to use it as above?
Thanks,
Boaz
EDIT: Thanks Timbo, it is indeed a duplicate (huh, I even chose the same class names). So lets get some added value: how about references? Is it possible / safe to do the following? :
class Child
{
public:
Child (Parent& parnet_ptr_) : parent_ptr(parent_ptr_) {};
private:
Parent* parent_ptr;
};
class Parent
{
public:
Parent() : child(*this) {};
private:
Child child;
}
The behaviour is well-defined so long as you don't attempt to dereference the pointer until after the
Parent
object has been completely constructed (as @Sergey says in a comment below, if the object being constructed is actually derived fromParent
, then all of its constructors must have completed).The parent
this
pointer, in "pointer terms", is well-defined (otherwise how would the parent constructor know on which instance is it operating?), but:Child
object aren't initialized yet;So, the parent object in general is still in an inconsistent state; everything the child object will do on construction on the parent object, will be done on a half-constructed object, and this in general isn't a good thing (e.g. if it calls "normal" methods - that rely on the fact that the object is fully constructed - you may get in "impossible" code paths).
Still, if all the child object do with the parent pointer in its constructor is to store it to be use it later (=> when it will be actually constructed), there's nothing wrong with it.
Yes. It's safe to use
this
pointer in initialization-list as long as it's not being used to access uninitialized members or virtual functions, directly or indirectly, as the object is not yet fully constructed. The objectchild
can store thethis
pointer ofParent
for later use!