• Advertisement

Archived

This topic is now archived and is closed to further replies.

Using "this" in ctr

This topic is 5000 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I''m curious as to what the problem with using "this" in the constructor of a class. MCVC 7.1 reports a warning when doing so, but I''ve seen alot of programs do this, and never have problems. Obviously you shouldnt call functions on a "this" pointer in the constructor, since technically the object isnt fully created yet, but whats wrong with using "this" as the initializer for a simple pointer?

Share this post


Link to post
Share on other sites
Advertisement
Isn''t anything wrong with storing the address, but if you deference it before some member hasn''t been initialized properly like another pointer it might blow up in your face, for example:


#include <iostream>

class example {
int* p;
example* _this;
public:
example() : _this(this) {
std::cout << *_this->p << std::endl;
}
};


Share this post


Link to post
Share on other sites
ok, thats what I thought, but ever so often someone tells me "DONT DO THAT".

thanks

Share this post


Link to post
Share on other sites
quote:
Original post by DrEvil
ok, thats what I thought, but ever so often someone tells me "DONT DO THAT".

thanks


Well as long as you don't call delete on that variable you should be okay because you might be tempted to do this:


class example {
example* _this;
public:
example() : _this(this) {
};

~example() { if(_this != 0) delete _this; }
};


int main() {

example* e = new eample();

delete e; //bang ur dead

}


[edited by - snk_kid on June 11, 2004 6:49:56 AM]

Share this post


Link to post
Share on other sites
To snk_kid''s code:
I think you may get a problem because *_this is not guaranteed
to be initialized with NULL. So it may be <>NULL without beeing
initialized in the constructor.
But perhaps I am wrong...

-Constantin

Share this post


Link to post
Share on other sites
quote:
Original post by conman
To snk_kid's code:
I think you may get a problem because *_this is not guaranteed
to be initialized with NULL. So it may be <>NULL without beeing
initialized in the constructor.
But perhaps I am wrong...

-Constantin


Thats the point i was making, it isn't refering to any instance, and it most likely contains some garbage value. Plus i wasn't deferencing _this, its deferencing _p;

[edited by - snk_kid on June 11, 2004 7:31:27 AM]

Share this post


Link to post
Share on other sites
Any time you access a member variable in a constructor to initialize it or whatever, you are implicity dereferencing the this pointer. For example:

class Foo
{
public:
Foo();

private:
int bar;
};

Foo::Foo()
{
bar = 17; // These two statements are equivalent

this->bar = 17;
}


And since you''re doing that in practically every class you make, you can safely assume that this != NULL.

Share this post


Link to post
Share on other sites
quote:
Original post by Aprosenf
Any time you access a member variable in a constructor to initialize it or whatever, you are implicity dereferencing the this pointer. For example:

class Foo
{
public:
Foo();

private:
int bar;
};

Foo::Foo()
{
bar = 17; // These two statements are equivalent

this->bar = 17;
}


And since you''re doing that in practically every class you make, you can safely assume that this != NULL.



Like i said already, i''m not talking about deferencing "this", i was talking about from my first example deferencing the pointer to int which hasn''t been intialized yet.

Share this post


Link to post
Share on other sites

  • Advertisement