Connecting Tech Pros Worldwide Help | Site Map

this in constructor

  #1  
Old July 19th, 2005, 08:11 PM
skscpp
Guest
 
Posts: n/a
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.


  #2  
Old July 19th, 2005, 08:11 PM
WW
Guest
 
Posts: n/a

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


  #3  
Old July 19th, 2005, 08:11 PM
lilburne
Guest
 
Posts: n/a

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


  #4  
Old July 19th, 2005, 08:12 PM
Sandeep
Guest
 
Posts: n/a

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
  #5  
Old July 19th, 2005, 08:12 PM
Ron Natalie
Guest
 
Posts: n/a

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.


  #6  
Old July 19th, 2005, 08:12 PM
Big Brian
Guest
 
Posts: n/a

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Using 'this' in constructor bb answers 2 October 18th, 2008 07:35 AM
Passing *this in constructor tech answers 7 June 27th, 2008 05:46 PM
memset 'this' in constructor? hack_tick answers 16 June 7th, 2006 07:25 AM
'x = this' in constructor Alex Vinokur answers 7 December 6th, 2005 07:05 PM