Connecting Tech Pros Worldwide Forums | Help | Site Map

this in constructor

skscpp
Guest
 
Posts: n/a
#1: Jul 19 '05
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.



WW
Guest
 
Posts: n/a
#2: Jul 19 '05

re: this in constructor


skscpp wrote:[color=blue]
> Is it unsafe to use 'this' in the constructor?[/color]

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

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

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

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.

--
WW aka Attila


lilburne
Guest
 
Posts: n/a
#3: Jul 19 '05

re: this in constructor


skscpp wrote:
[color=blue]
> 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?
> }
>[/color]

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();
}


Sandeep
Guest
 
Posts: n/a
#4: Jul 19 '05

re: this in constructor


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/RealtimeMa...erformance.htm

Sandeep
--
http://www.EventHelix.com/EventStudio
EventStudio 2.0 - Generate Sequence Diagrams and Use Cases in PDF
Ron Natalie
Guest
 
Posts: n/a
#5: Jul 19 '05

re: this in constructor



"skscpp" <sksjava@hotmail.com> wrote in message news:bmf87d$lpq$1@news.lsil.com...[color=blue]
> Is it unsafe to use 'this' in the constructor? Is there any book out there
> (Stroustrup or Meyers or Sutter) that talks about this?[/color]

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.


Big Brian
Guest
 
Posts: n/a
#6: Jul 19 '05

re: this in constructor


eventhelix@yahoo.com (Sandeep) wrote in message news:<902d33b7.0310131738.224bd394@posting.google. com>...[color=blue]
> 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/RealtimeMa...erformance.htm
>
> Sandeep[/color]


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
Closed Thread