constructor and initializer

V

Vincent RICHOMME

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 ?
 
T

TB

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
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,902
Latest member
Elena68X5

Latest Threads

Top