Jump to content
  • Advertisement
Sign in to follow this  
aaronz

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

This topic is 3723 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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!

Share this post


Link to post
Share on other sites
Advertisement
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);

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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)
{
}
};

Share this post


Link to post
Share on other sites
You should dispose of your current "C++" book if it does not cover initializer lists and get a more recent one, like Accelerated C++.

Share this post


Link to post
Share on other sites
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,

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!