0

Possible Duplicate:
What is this weird colon-member syntax in the constructor?

I need to know what this code means:

Cman::Cman() :
m_pThread (NULL)  //m_pThread is pointer to other class
{
}

I don't really understand this code. What does this NULL mean ? Can I use it as a regular constructor ?

Community
  • 1
  • 1
adir
  • 1,257
  • 2
  • 16
  • 22
  • 1
    and the initialization of m_pThread is done in the initializer list, see http://www.parashift.com/c++-faq-lite/ctors.html#faq-10.6 for more info. – Max Feb 09 '11 at 20:09

6 Answers6

5

It says to initialize m_pThread to NULL before the code inside the constructor is executed.

Blrfl
  • 6,817
  • 1
  • 25
  • 25
5

This is the default constructor for the class Cman. It will initialize the value of m_pThread, presumably a private member for the class that is a pointer, to NULL. NULL is assigned to a pointer when it doesn't point to anything useful (rather than a random value you can't test for).

Mark Loeser
  • 17,657
  • 2
  • 26
  • 34
3

This is a default constructor for class Cman, which initializes a member called m_pThread (which is a pointer), with the value NULL. NULL in this context means nothing, empty or 0, fundamentally it is an invalid pointer (which points to nothing).

Nim
  • 33,299
  • 2
  • 62
  • 101
3
Cman::Cman() : m_pThread(NULL) { }

Things after the : is called initializer-list. It's used to initialize the member variables. So the syntax m_pThread(NULL) initializes m_pThread with NULL. You can use it to initialize all your member-variables.

Example,

class Cman
{
   Thread *m_pThread;
   int Duration;
   int MemorySize;
   public:
   Cman() : m_pThread(NULL), Duration(10000), MemorySize(9999)
   { 
   }  
};

Remember, there is a difference between initialization and assignment.

Read about it here: Using Initialization Lists to Initialize Fields

Nawaz
  • 353,942
  • 115
  • 666
  • 851
2
Cman::Cman() : m_pThread(NULL) { }

is complete equivalent for:

Cman::Cman() { m_pThread = NULL; }

NULL itself is null pointer

Andrey
  • 59,039
  • 12
  • 119
  • 163
  • In this case yes, but in more complex cases when its an object, it isn't. – Mark Loeser Feb 09 '11 at 20:09
  • @Mark Loeser could you give example? – Andrey Feb 09 '11 at 20:10
  • @Andrey: Sure, if the variable you are initializing is an object (say a string), the string will be default constructed, and then you'll be using the assignment operator inside of the constructor, instead of constructing it the first time with the data. – Mark Loeser Feb 09 '11 at 20:11
  • You are wrong this is not equivalent. In first case during construction m_pThread sets to NULL in second case after object construction it sets to NULL. More precise in first case we can't write this->m_pThread (it will cause warning or error) in second case we can. – Mihran Hovsepyan Feb 09 '11 at 20:12
  • @Mihran Hovsepyan any proof that I am wrong? here is proof that i am right. here are two definitions http://pastebin.com/BD1LmMhZ here is assembly generated by MSVC for first case http://pastebin.com/bhjWckRg here for second http://pastebin.com/hTirTPRf . – Andrey Feb 09 '11 at 20:32
  • @Andrey You are right! In both cases generated Assemblers are same. But this can be cause of compiler optimisation. Because in other case they can be different. For example in case when we should construct uncopyable object we can't use nothing else than initiliser-list. – Mihran Hovsepyan Feb 09 '11 at 20:38
  • 1
    Certain member variables cannot be initialized in the constructor body and MUST use member initialization, such as references and members of class type with no default constructor. – Fred Larson Feb 09 '11 at 21:49
  • 1
    @Mihran, @Andrey: For primitives, like a pointer or int (etc), it'll likely be exactly the same. The real important times to use the initialization list is when you have constant members, or members that would be silly to default construct, and then assign to rather than construct properly in the first place. – Mark Loeser Feb 10 '11 at 01:14
1

This is simple constructor of Cman class, which uses list of initializations to initialize m_pThread and sets its value to NULL. In most cases NULL is macro defined this way

#define NULL 0
or
#define NULL 0L

After this initialization your pointer points to address 0x000000.

Mihran Hovsepyan
  • 10,810
  • 14
  • 61
  • 111