views:

146

answers:

4

Well, I'm gonna be pretty straightforward here, I just have a piece of code in c++ which I'm not sure I really understand and need some help with.

Ok, to simplify lets just say I have a class that is defined like this: (the real class is a little bit more complicated, but this is what matters)

class myClass : public Runnable {
    Semaphore *m_pMySemaphore;
    __Queue<Requests> *m_pQueue;
    Request m_Request;
    VetorSlotBuffer *m_vetorSlotBuffer;
}

Up to here nothing is wrong, myClass is just a regular class which has 3 members that actually are pointers to other classes and an object of the class Request, the implementation of those classes not being important for my point here.

Then when this person implemented the constructor for myClass he or she did this:

myClass::myClass() : m_pMySemaphore(0), m_pQueue(0), m_vetorSlotBuffer(0) { }

It's pretty evident that those three variables are treated like that by the constructor because they are pointers, am I right? but what kind of syntax is that? am I setting the pointers to null by doing that? I've seen a little bit of c++ already but never found something like that.

And secondly, what's the deal with the ":" after the constructor declaration? that I've seen but never took the time to investigate. Is this like an inner class or something?

Thank you very much in advance. Nelson R. Perez

+3  A: 

Yes, pointers set to 0 are pointers set to null.

The : syntax for constructors is called initialization lists. It's used here for initializing member fields in a simple, direct manner. Should be easy to google with the proper terminology.

Tanzelax
Exactly, but that was the problem, I didn't even know they had a name for that!! but thank you very much!! I understand it now.
Bilthon
@Bilthon, yeah, so many things are hard to search about without the proper terminology... especially strange syntax symbols in programming languages. `#` preprocessor directives, `<>` templating, and a whole slew of other things are just as bad. :p
Tanzelax
+4  A: 

It's member's initialization on object creation, called initialization list.
You can't initialize them elsewhere, say:

class X { 
    int i = 0; // error bad syntax
} 

Neither in constructor by use of assignment, if they're const :

class X { 
    const int i; 
    X(){ 
        i = 0; // error it's a const
    } 
} 

So the c++ guys made up that syntax!

clyfe
+4  A: 

Yes, those pointers are being initialized to NULL. The key thing here is that they are initialized, as opposed to assigned. I.e.:

myClass::myClass()
  :  m_pMySemaphore(0),   // initialized to 0
     m_pQueue(NULL)      // initialized to NULL (preferable to 0)
                         // no initializer for m_Request - uses default constructor
                         // no initializer for m_vectorSlotBuffer
{
   // at this point, the object is entirely initialized, and can be used.

   // this next line ASSIGNS to the member
   m_vectorSlotBuffer = 0;
}

What happens to m_vectorSlotBuffer, is that it is initialized, and then assigned, in two separate steps. By doing it like m_pQueue, we save a step, and initialize properly. This becomes very important when we want to use non-default constructor, or if we want to initialize const members.

Also, the : after the constructor () is what begins the initialization section, before we get to the { where we enter the body of the constructor.

Finally, NULL is not guaranteed to be 0. NULL is portable, but most architectures use 0 as NULL.

EDIT: 0/NULL distinction is not meaningful anymore. Just pick one and be consistent.

Tim
But in later versions of the C and C++ specifications, 0 is defined to be NULL. In other words, even if the architecture uses some other bit pattern for a NULL pointer, the compiler is required to convert pointer = 0 into pointer = NULL.
Zan Lynx
+1 for distinguishing between initializing and assigning. Almost -1'ed you again though, since the 0/NULL bit is incorrect. As @Zan says, C++ defines NULL as 0. The actual machine representation may be different, but the constant integer 0 *always* converts to a null pointer.
jalf
+8  A: 

This is an initialization list

And it's the recommended way to initialize your members