this in constructor

S

skscpp

Is it unsafe to use 'this' in the constructor? Is there any book out there
(Stroustrup or Meyers or Sutter) that talks about this?

// small example - not compiled code
Test::Test(int a)
{
this->m_A = a; // I have seen this before
someClass->registerMe(this); // Is this okay also?
}


Thanks.
 
W

WW

skscpp said:
Is it unsafe to use 'this' in the constructor?

No, it isn't. But you have to remember that (for example) reading
uninitialized members is an error.
Is there any book out
there (Stroustrup or Meyers or Sutter) that talks about this?

Uh. I don't recall.
// small example - not compiled code
Test::Test(int a)
{
this->m_A = a; // I have seen this before

This is surely OK, if m_A is indeed a member.
someClass->registerMe(this); // Is this okay also?

Well, it depends. If this is the last line (part) of your code in the
constructor (so your object is already fully constructed) it is OK. So if
you have failed to initialize a member, which is the accessed by registerMe
(or by something it calls(, that will indeed be a problem. But that can
also be done without "calling out" of the class/object:

struct Bang {
Bang() { incMe(); }
void incMe() {
++m_i;
}
private:
int m_i;
};

Since m_i is not initialized ++m_i; is undefined behavior.
 
L

lilburne

skscpp said:
Is it unsafe to use 'this' in the constructor? Is there any book out there
(Stroustrup or Meyers or Sutter) that talks about this?

// small example - not compiled code
Test::Test(int a)
{
this->m_A = a; // I have seen this before
someClass->registerMe(this); // Is this okay also?
}

You'll have problems if someClass::registerMe() calls any
virtuals because the derived classes wont have been
constructed - the following will crash.

class A;
void somefunc(A* a);

class A {
public:
A() {
somefunc(this);
}
virtual void vfunc() = 0;
};

class B : public A {
public:
B() {}
virtual void vfunc() {};
};

int main()
{
B b;
return 0;
}

void somefunc(A* a)
{
a->vfunc();
}
 
R

Ron Natalie

skscpp said:
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.
 
B

Big Brian

When you are initializing member variables in a constructor, you
are implicitly using "this". It is safe to use it.

See the following article for the mapping of contstructor code
to C. This should clarify the role of "this".

http://www.eventhelix.com/RealtimeMantra/basics/ComparingCPPAndCPerformance.htm

Sandeep


But in the original post, "this" is being used for more than
initializing member variables.

someClass->registerMe(this);

Who knows what this function actually does. It could be calling
this->( some virtual function ). In this case, there would be
problems.

-Brian
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top