I have an object in my class that only has one constructor, and this constructor takes a pointer to an interface implemented by my class. So I want to put it on the initializer list:
: thatObject(this)
But that gives me a warning and I can understand why giving a pointer to an unconstructed class is not a good idea. So the question is, what do I do? Should I do:
: thatObject(NULL)
{
thatObject = TheClass(this);
}
What is the proper way of dealing with this?
Thanks
If
thatObjectis guaranteed to never dereference the given pointer until after it’s constructor is complete, then ignore or suppress the warning. If you don’t feel safe guaranteeing that it will never in the future dereference the pointer, than go with the second option.In MSVC the code to supress a warning is:
GCC is more complicated: (and untested, I don’t have GCC)