this in constructor

Discussion in 'C++' started by skscpp, Oct 13, 2003.

  1. skscpp

    skscpp Guest

    Is it unsafe to use 'this' in the constructor? Is there any book out there
    (Stroustrup or Meyers or Sutter) that talks about this?

    // small example - not compiled code
    Test::Test(int a)
    {
    this->m_A = a; // I have seen this before
    someClass->registerMe(this); // Is this okay also?
    }


    Thanks.
     
    skscpp, Oct 13, 2003
    #1
    1. Advertising

  2. skscpp

    WW Guest

    skscpp wrote:
    > Is it unsafe to use 'this' in the constructor?


    No, it isn't. But you have to remember that (for example) reading
    uninitialized members is an error.

    > Is there any book out
    > there (Stroustrup or Meyers or Sutter) that talks about this?


    Uh. I don't recall.

    > // small example - not compiled code
    > Test::Test(int a)
    > {
    > this->m_A = a; // I have seen this before


    This is surely OK, if m_A is indeed a member.

    > someClass->registerMe(this); // Is this okay also?


    Well, it depends. If this is the last line (part) of your code in the
    constructor (so your object is already fully constructed) it is OK. So if
    you have failed to initialize a member, which is the accessed by registerMe
    (or by something it calls(, that will indeed be a problem. But that can
    also be done without "calling out" of the class/object:

    struct Bang {
    Bang() { incMe(); }
    void incMe() {
    ++m_i;
    }
    private:
    int m_i;
    };

    Since m_i is not initialized ++m_i; is undefined behavior.

    --
    WW aka Attila
     
    WW, Oct 13, 2003
    #2
    1. Advertising

  3. skscpp

    lilburne Guest

    skscpp wrote:

    > Is it unsafe to use 'this' in the constructor? Is there any book out there
    > (Stroustrup or Meyers or Sutter) that talks about this?
    >
    > // small example - not compiled code
    > Test::Test(int a)
    > {
    > this->m_A = a; // I have seen this before
    > someClass->registerMe(this); // Is this okay also?
    > }
    >


    You'll have problems if someClass::registerMe() calls any
    virtuals because the derived classes wont have been
    constructed - the following will crash.

    class A;
    void somefunc(A* a);

    class A {
    public:
    A() {
    somefunc(this);
    }
    virtual void vfunc() = 0;
    };

    class B : public A {
    public:
    B() {}
    virtual void vfunc() {};
    };

    int main()
    {
    B b;
    return 0;
    }

    void somefunc(A* a)
    {
    a->vfunc();
    }
     
    lilburne, Oct 14, 2003
    #3
  4. skscpp

    Sandeep Guest

    Sandeep, Oct 14, 2003
    #4
  5. skscpp

    Ron Natalie Guest

    "skscpp" <> wrote in message news:bmf87d$lpq$...
    > Is it unsafe to use 'this' in the constructor? Is there any book out there
    > (Stroustrup or Meyers or Sutter) that talks about this?


    It's not unsafe as long as you are cognizant of what parts of the current
    object have already been constructed. In this case, you are using this
    to set an initial value which would appear to be OK (I assume m_A is an
    int member of Test).

    Of course:
    this->m_A = a;
    in that case is the same as
    m_A = a;

    Even then, it would be better to INITIALIZE m_A rather than assigning to
    it afterwards:

    Test::Test(int a) : m_A(a) {
    someClass->registerMe(this);
    }

    As for the call to registerMe, it should be noted that Test (or whatever object
    that Test is a base class for), is not yet fully constructed. If all it does is
    remember the pointer and not use it until after the constructors finish, then
    all is OK again.
     
    Ron Natalie, Oct 14, 2003
    #5
  6. skscpp

    Big Brian Guest

    (Sandeep) wrote in message news:<>...
    > When you are initializing member variables in a constructor, you
    > are implicitly using "this". It is safe to use it.
    >
    > See the following article for the mapping of contstructor code
    > to C. This should clarify the role of "this".
    >
    > http://www.eventhelix.com/RealtimeMantra/basics/ComparingCPPAndCPerformance.htm
    >
    > Sandeep



    But in the original post, "this" is being used for more than
    initializing member variables.

    someClass->registerMe(this);

    Who knows what this function actually does. It could be calling
    this->( some virtual function ). In this case, there would be
    problems.

    -Brian
     
    Big Brian, Oct 14, 2003
    #6
    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. Giulio
    Replies:
    9
    Views:
    1,068
    Patrick Kowalzick
    Jun 25, 2003
  2. Brett Irving
    Replies:
    3
    Views:
    3,352
    John Harrison
    Jun 29, 2003
  3. lallous
    Replies:
    5
    Views:
    8,876
    David Harmon
    Jan 23, 2004
  4. Aire
    Replies:
    3
    Views:
    489
    Mike Wahler
    Jan 25, 2004
  5. Generic Usenet Account
    Replies:
    10
    Views:
    2,353
Loading...

Share This Page