Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Validity of the this pointer before the object is constructed

Reply
Thread Tools

Validity of the this pointer before the object is constructed

 
 
tuko
Guest
Posts: n/a
 
      09-16-2004
Hello everyone.

At the following snippet, at the commented line
the [this] pointer is valid? I mean the object bar is not
constructed yet. So after the contruction of the bar object,
the list foo::bars_, contain valid pointers? Is that
guarantee to work?

Suppose that no exception is thrown.

#include <list>
class bar;
class foo {
public:
std::list<bar *> bars_;
};
class bar {
public:
bar(foo *f);
};

bar::bar(foo *f) {
if (f) {
f->bars_.push_back(this); // The this pointer is valid?
}
}

int main () {
foo *f=new foo;
bar *br = new bar(f);
}

Many thanks for your time

--
tuko. the mexican.
 
Reply With Quote
 
 
 
 
JKop
Guest
Posts: n/a
 
      09-16-2004
tuko posted:

> Hello everyone.
>
> At the following snippet, at the commented line
> the [this] pointer is valid? I mean the object bar is not
> constructed yet. So after the contruction of the bar

object,
> the list foo::bars_, contain valid pointers? Is that
> guarantee to work?
>
> Suppose that no exception is thrown.
>
> #include <list>
> class bar;
> class foo {
> public:
> std::list<bar *> bars_;
> };
> class bar {
> public:
> bar(foo *f);
> };
>
> bar::bar(foo *f) {
> if (f) {
> f->bars_.push_back(this); // The this pointer

is valid?
> }
> }
>
> int main () {
> foo *f=new foo;
> bar *br = new bar(f);
> }
>
> Many thanks for your time
>
> --
> tuko. the mexican.
>


Without delving too far into it, it seems to me that if you
get a chance at all to use "this", then it's valid,
including within constructors and destructors!

-JKop
 
Reply With Quote
 
 
 
 
Richard Herring
Guest
Posts: n/a
 
      09-16-2004
In message <C9h2d.28127$(E-Mail Removed)>, JKop <(E-Mail Removed)>
writes
>tuko posted:
>
>> Hello everyone.
>>
>> At the following snippet, at the commented line
>> the [this] pointer is valid? I mean the object bar is not
>> constructed yet. So after the contruction of the bar

>object,
>> the list foo::bars_, contain valid pointers? Is that
>> guarantee to work?
>>
>> Suppose that no exception is thrown.
>>
>> #include <list>
>> class bar;
>> class foo {
>> public:
>> std::list<bar *> bars_;
>> };
>> class bar {
>> public:
>> bar(foo *f);
>> };
>>
>> bar::bar(foo *f) {
>> if (f) {
>> f->bars_.push_back(this); // The this pointer

>is valid?


Depends what you mean by "valid". It points to the location where a bar
is being constructed, and that location won't change. By the time you
come to dereference it, somewhere in a member function of foo, the
object will have been fully constructed. So, that use of it is valid.

>> }
>> }
>>
>> int main () {
>> foo *f=new foo;
>> bar *br = new bar(f);
>> }
>>
>> Many thanks for your time
>>

>
>Without delving too far into it, it seems to me that if you
>get a chance at all to use "this", then it's valid,
>including within constructors and destructors!


Again, it depends what you mean by "valid". If the thing being
constructed is a derived class, then within its constructor the dynamic
type of "this" is still that of the base, so calling virtual functions
may not have the effect you expect.

--
Richard Herring
 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      09-16-2004
tuko wrote:
> At the following snippet, at the commented line
> the [this] pointer is valid? I mean the object bar is not
> constructed yet. So after the contruction of the bar object,
> the list foo::bars_, contain valid pointers? Is that
> guarantee to work?


Yes.

>
> Suppose that no exception is thrown.
>
> #include <list>
> class bar;
> class foo {
> public:
> std::list<bar *> bars_;
> };
> class bar {
> public:
> bar(foo *f);
> };
>
> bar::bar(foo *f) {
> if (f) {
> f->bars_.push_back(this); // The this pointer is valid?


Yes. The memory has been allocated, the pointer is valid.

> }
> }
>
> int main () {
> foo *f=new foo;
> bar *br = new bar(f);
> }


V
 
Reply With Quote
 
Dave Rahardja
Guest
Posts: n/a
 
      09-17-2004
JKop wrote:

> Without delving too far into it, it seems to me that if you
> get a chance at all to use "this", then it's valid,
> including within constructors and destructors!
>
> -JKop


Careful, careful...

http://www.parashift.com/c++-faq-lit....html#faq-10.7
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Global object not being constructed kempshall ASP .Net 0 08-30-2007 05:03 PM
Pointer to a Partially-Constructed Object better_cs_now@yahoo.com C++ 1 05-28-2007 08:40 PM
gcc needs copy consturctor for inplace constructed object passed to func as reference, others not nutty C++ 6 06-15-2006 02:02 PM
delete resouce of partial constructed object Metaosp C++ 2 02-06-2006 03:21 PM
validity of using subclasses in pointer-to-pointer types? Marcus Alanen C++ 1 09-07-2003 10:42 AM



Advertisments