Validity of the this pointer before the object is constructed

Discussion in 'C++' started by tuko, Sep 16, 2004.

  1. tuko

    tuko Guest

    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.
     
    tuko, Sep 16, 2004
    #1
    1. Advertising

  2. tuko

    JKop Guest

    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
     
    JKop, Sep 16, 2004
    #2
    1. Advertising

  3. In message <C9h2d.28127$>, JKop <>
    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
     
    Richard Herring, Sep 16, 2004
    #3
  4. 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
     
    Victor Bazarov, Sep 16, 2004
    #4
  5. Dave Rahardja, Sep 17, 2004
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Marcus Alanen
    Replies:
    1
    Views:
    341
    Alf P. Steinbach
    Sep 7, 2003
  2. Metaosp
    Replies:
    2
    Views:
    326
    mlimber
    Feb 6, 2006
  3. nutty
    Replies:
    6
    Views:
    413
    Tom Widmer
    Jun 15, 2006
  4. Replies:
    1
    Views:
    289
    Rolf Magnus
    May 28, 2007
  5. Alf P. Steinbach
    Replies:
    5
    Views:
    167
    Alf P. Steinbach
    Nov 24, 2013
Loading...

Share This Page