Using "this" in ctr

Started by
6 comments, last by DrEvil 19 years, 10 months ago
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?
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;   }};


ok, thats what I thought, but ever so often someone tells me "DONT DO THAT".

thanks
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]
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
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]
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.
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.

This topic is closed to new replies.

Advertisement