Sign in to follow this  
sbayeta

What does this sintax mean ?

Recommended Posts

sbayeta    122
Hi, I'm new to C++, and I'm reading a WIN32 programming tutorial using C++. I have a good C background (I've been programming C for a couple of years). I'd like to know what does this sintax mean:
class WinSimpleClass
{
public:
	WinSimpleClass (char const * name, HINSTANCE hInst)
		: _name (name), _hInstance (hInst)  //this is what I don't understand
	{}
	WinSimpleClass (int resId, HINSTANCE hInst);
	char const * GetName () const { return _name.c_str (); }
	HINSTANCE GetInstance () const { return _hInstance; }
    HWND GetRunningWindow ();
protected:
	HINSTANCE	_hInstance;
	std::string	_name;
};
The part after the colon in the declaration of the constructor. I've also seen that sintax in other methods as well. Thanks in advance for any help.

Share this post


Link to post
Share on other sites
T2k    220
thats how you call constructors of baseclasses or member variables in your constructor, you could also write:

WinSimpleClass (char const * name, HINSTANCE hInst){
_name= name;
_hInstance= hInst;
};


i think it will produce the same code, but iam not sure.


T2k

Share this post


Link to post
Share on other sites
DrPizza    160
It's an initialization list. It initializes those members to the values specified. This is different from writing membername = expression; within the body of the constructor, as that works by default initializing and then assigning.


Incidentally, the word is "syntax", not "sintax". "sintax" would be having to pay to masturbate.

Share this post


Link to post
Share on other sites
marijnh    182
WinSimpleClass (char const * name, HINSTANCE hInst)
: _name (name), _hInstance (hInst)

This is an initializer list, it is allowed between the constructor name and the constructor body of a class. What it does is initialize member variable _name to name and _hInstance to hInst. This is often preferable to assigning the values in the body of the constructor - it looks cleaner (imho) and for const members or references it is the only way to initialize them (they can not be assigned to).

(edit: doh, easy questions get answered very quick on here)

Share this post


Link to post
Share on other sites
Fruny    1658
Quote:
Original post by T2k
i think it will produce the same code, but iam not sure.


Odds are that it will not, except in the very simplest cases.

Share this post


Link to post
Share on other sites
petewood    819
For example, if a class has no default constructor and requires something to be passed to it to create it correctly, what happens if it is a member of another class?


class NoDefault {
int size;
public:
NoDefault(int size);//need to pass integer to create it
};

//this class doesn't compile
class SomeClass {
NoDefault no_default;
public:
SomeClass()
{
//no_default must already have been created by the time you reach here
}
};


All class members are created before you reach the body of the constructor. For those that need some initialisation variables you can use the initialisation list. You can do this with any members, not just those without default constructors. It is more efficient.

class SomeClass {
NoDefault no_default;
public:
SomeClass() : no_default(5)
{
//no_default must already have been created by the time you reach here
}
};

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this