C++ problem: What does this ":" mean?

Started by
9 comments, last by aaronz 16 years, 1 month ago
Hi there, I've come across some strange code like this:

ViewPlane::ViewPlane(void)							
	 : 	hres(400), 
		vres(400),
		s(1.0),
		gamma(1.0),
		inv_gamma(1.0),
		show_out_of_gamut(false)
{}

I don't quite understand what does the ":" mean after the ViewPlane(void). It looks like those fuction after the ":" like hres(400) and vres(400) should go inside the {}. This doesn't only happen in constructors, here is another one:

wxraytracerFrame::wxraytracerFrame(const wxPoint& pos, const wxSize& size)
                : wxFrame((wxFrame *)NULL, -1, wxT( "Ray Tracer" ), pos, size)
{
   wxMenu *menuFile = new wxMenu;
   
   menuFile->Append(Menu_File_Open, wxT("&Open..."   ));
   menuFile->Append(Menu_File_Save, wxT("&Save As..."));
   menuFile->AppendSeparator();
   ...
}

Isn't the ":" supposed to be some kind of inheritance like "class Y : public X"? I think I need to study some more C++ fundamentals... Thanks alot!
Advertisement
It only happens in constructors. Those are initializer lists.
oh yes ... right, this only happens in constructors, thanks man
It's the initializer list for the member variables of the object (http://msdn2.microsoft.com/en-us/library/dtey3c73.aspx). It's basically equivelent to doing the initialization in the constructor body (like this:)

ViewPlane::ViewPlane(void)
{
hres = 400;
vres = 400;
s = 1.0;
gamma = 1.0;
inv_gamma = 1.0;
show_out_of_gamut = false;
}

but can be potentially more efficient



Both functions you listed are constructors, the second one (xraytracerFrame::wxraytracerFrame) is a constructor that takes arguments

wxraytracerFrame* pWXTFrame;
wxPoint posPt; // these would be initialized but I don't know the class
wxSize sz; // these would be initialized but I don't know the class

pWXTFrame = new xraytracerFrame(posPt, sz);

Just to add something to the above. Using the member initialisation list is actually INITIALISATION, where as setting things in the constructor body is ASSIGNMENT, as you use the assignment operator '='.

Note that by default all members will be initialised before the constructor body (using its default constructor) even if you don't use the initialisation list. So the reason using the initialisation list could be more efficient is because otherwise you will be assigning after the default initialisation anyway.

Hope that helps.
One really good feature of the initializer list is that you can initialize const members with it. Say you had a const int count as a member. You want this field to remain constant after the class is instantiated, but you also want to specify the initial value. You can't set the desired value even in the constructor. But you can do so using the initializer list. This is how it would work:

class MyClass
{
const int count;

public:
MyClass(int val):count(val)
{
}
};
You should dispose of your current "C++" book if it does not cover initializer lists and get a more recent one, like Accelerated C++.
Hi,

I can see that a lot of people have already given you good answers.. and here are some more.

The person that ":" is only used in a constructor is wrong.. it's used with inheritance:

class CMyObject : public CMyOtherObject

It's used for bit fields:
struct Date
{
unsigned nWeekDay : 3;
unsigned nMonthDay : 6;
unsigned nMonth : 5;
unsigned nYear : 8;
};

it's used for conditional else
bool a = cat==dog?false:true;

it's used for labels (if you want to use goto)
mylabel:

but in your example it's used to initialize the member variables before entering the constructor. As one other person pointed out .. it's good to initialize consts... and it's also good to initialize member variables that are references (which must have a reference):
CMyObject& m_myobj;

hope this helped,

http://osghelp.com - great place to get OpenScenGraph help
Since noone's linked the C++ FAQ Lite yet...

[Edited by - TheUnbeliever on March 14, 2008 10:20:58 AM]
[TheUnbeliever]
Just to show why we would use member initializers, say we have a class declared as
follows with a string variable and a constructor:

#include <string>using std::string;class Name{   private:     string name;   public:   Name( string & n )   {       this->name = n;    }};


When the object is first created (before the body of the constructor is called) C++ will call the default constructor for the member variable 'name'. This happens for all non-primitive types (objects) that are members of a class.

Then you are assigning the parameter 'n' to this variable later on too. So essentially you are initializing the same variable twice, which is slightly inefficient.

Using the member initializer however, bypasses this default initialization of 'name':

#include <string>using std::string;class Name{   private:     string name;   public:   Name( string & n ) : name(n)   {    }};


Now 'name' is initialized with the value 'n' before the constructor is called, and default constructor for 'name' is not called. So you have saved one assignment to name in the constructor and made your constructor code more efficient.

This topic is closed to new replies.

Advertisement