constructor and initializer

Discussion in 'C++' started by Vincent RICHOMME, Jan 14, 2006.

  1. Hi,

    Let' say I have a class with an object pointer as member something like:

    // classA.h
    class A
    {
    A(B* pB);
    B* m_pB;
    };

    //classA.cpp

    class A:A(B* pB):
    m_pB(NULL)
    {
    m_pB = pB;
    if (m_pB == NULL)
    return;
    }

    so I want to be sure m_pB is not NULL, so I am initializing it in
    initialization list with NULL and after I am checking its value.
    But my question is what is done first, the initialization(m_pB=NULL) or
    the declaration in the body ?
    Vincent RICHOMME, Jan 14, 2006
    #1
    1. Advertising

  2. Vincent RICHOMME

    TB Guest

    Vincent RICHOMME sade:
    > Hi,
    >
    > Let' say I have a class with an object pointer as member something like:
    >
    > // classA.h
    > class A
    > {
    > A(B* pB);
    > B* m_pB;
    > };
    >
    > //classA.cpp
    >
    > class A:A(B* pB):


    A::A(B* pB):

    > m_pB(NULL)
    > {
    > m_pB = pB;
    > if (m_pB == NULL)
    > return;
    > }
    >
    > so I want to be sure m_pB is not NULL, so I am initializing it in
    > initialization list with NULL and after I am checking its value.
    > But my question is what is done first, the initialization(m_pB=NULL) or
    > the declaration in the body ?


    The constructor initializer list is executed before the constructor body.

    TB
    TB, Jan 14, 2006
    #2
    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. Razvan
    Replies:
    7
    Views:
    17,569
    Lee Fesperman
    Jul 4, 2004
  2. Ed Thompson

    static initializer vs constructor

    Ed Thompson, Oct 9, 2004, in forum: Java
    Replies:
    9
    Views:
    920
    Chris Uppal
    Oct 11, 2004
  3. Matthias Kaeppler

    Initializer vs. Constructor assignment

    Matthias Kaeppler, May 7, 2005, in forum: Java
    Replies:
    4
    Views:
    1,011
    Tor Iver Wilhelmsen
    May 8, 2005
  4. Jess
    Replies:
    8
    Views:
    342
  5. Generic Usenet Account
    Replies:
    10
    Views:
    2,224
Loading...

Share This Page