Validity of the this pointer before the object is constructed

T

tuko

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
 
J

JKop

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

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
 
R

Richard Herring

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?[/QUOTE]

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.
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.
 
V

Victor Bazarov

tuko said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top