What does this sintax mean ?

Started by
5 comments, last by sbayeta 19 years, 9 months ago
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.
Advertisement
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
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.
char a[99999],*p=a;int main(int c,char**V){char*v=c>0?1[V]:(char*)V;if(c>=0)for(;*v&&93!=*v;){62==*v&&++p||60==*v&&--p||43==*v&&++*p||45==*v&&--*p||44==*v&&(*p=getchar())||46==*v&&putchar(*p)||91==*v&&(*p&&main(0,(char**)(--v+2))||(v=(char*)main(-1,(char**)++v)-1));++v;}else for(c=1;c;c+=(91==*v)-(93==*v),++v);return(int)v;}  /*** drpizza@battleaxe.net ***/
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)
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.
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
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 compileclass 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    }};
Thanks a lot to all.

That was very informative. Specially the "sintax"-"syntax" thing.

Cheers!

This topic is closed to new replies.

Advertisement