"skscpp" <sk*****@hotmail.com> wrote in message news:bm**********@news.lsil.com...
Is it unsafe to use 'this' in the constructor? Is there any book out there
(Stroustrup or Meyers or Sutter) that talks about this?
It's not unsafe as long as you are cognizant of what parts of the current
object have already been constructed. In this case, you are using this
to set an initial value which would appear to be OK (I assume m_A is an
int member of Test).
Of course:
this->m_A = a;
in that case is the same as
m_A = a;
Even then, it would be better to INITIALIZE m_A rather than assigning to
it afterwards:
Test::Test(int a) : m_A(a) {
someClass->registerMe(this);
}
As for the call to registerMe, it should be noted that Test (or whatever object
that Test is a base class for), is not yet fully constructed. If all it does is
remember the pointer and not use it until after the constructors finish, then
all is OK again.