473,473 Members | 2,073 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

constructor and initializer

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 ?
Jan 14 '06 #1
1 1857
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
Jan 14 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Chris K | last post by:
I am relatively new to C++ and hope that this question is relevant. I have spent some time at the local library and some time on dejanews, but have no decided to go ahead with my question, since...
15
by: Alfonso Morra | last post by:
Hi, I have some code from an example, that I want to retrofit into my project. The code from the example has the following line: SharedAppenderPtr myAppender( new...
9
by: Player | last post by:
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Hello all. I am in the process of teaching myself C# and I think I am doing OK. I have learnt how to how to call the right constructor of a...
5
by: Pallav singh | last post by:
How can we justify that initializer list is better in performance than assignment list in constructor of C++ ??
14
by: Dan Rumney | last post by:
I've been taking a look at Douglas Crockford's JSLint. One of the conventions that it expects is that arrays be created using literal notation var arr1 = ; as opposed to using a constructor...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.