syntax question

Started by
4 comments, last by wubo 21 years, 10 months ago
i''ve been reading through the "Creating a Scripting System" series and i''m seeing a convention i''ve never seen before. i can''t think of a way to phrase it into a google search, so this forum is my only recourse. this is the instruction class, it encapsulates an enumerated opcode and its optional data component, not yet implemented. the part that is throwing me off is the : (colon) in the constructor... what the heck is that for? and the _code(code), how is that legal? looks like a function but it''s just an enum... i''m at a serious loss here. i''d appreciate any help // the basic instruction, currently just encapsulating an opcode class Instruction { public: Instruction(opcode code) : _code(code) {} opcode Code() const { return _code; } private: opcode _code; //char* _data; // additional data, currently not used };
Advertisement
What you see is the constructors initialize-list (I know my spelling sux). And the function call thing is a call to the _codes constructor taking an opcode. The reason for using the initlist is that you only have one construction call for that member. If you initialize the member inside the constructors body the member default constructor will already hav been called.
Hope that helped (I know I´m not the best at explaining things)

[edited by - ArchMiffo on June 7, 2002 4:23:52 AM]
basically it''s just another way of writing:

Instruction(opcode code)
{
_code = code;
}


You can use Constructor(PrivateDataType DataPassedIn) : PrivateData(DataPassedIn) as a way to set the private data of the function equal to the DataPassedIn without writing it in the body of the function. It''s just so that you can easily keep initializing private data away from other stuff you might do in the body.

He who laughs, lasts
-Dan- Can't never could do anything | DansKingdom.com | Dynamic Particle System Framework for XNA
This is an initialisation list.
alright, thanks! it is all very clear now. i appreciate the help immensely
deadlydog:

An initialization list is not the same as assignment within the constructor.


For built-in types, long, short, etc, it may not be a big deal, but for classes it makes a big difference.


An initialization list is an opportunity to give parameters to a constructor of a base class or member object, as well as initialize built-in types.

Note that this process is ALWAYS done even if you don''t put anything there - it invokes default ctors or does nothing for built-in types.

This is significant because it can affect the speed of a constructor as you are doing more work than is necessary. Even for built-in types.

For a class type it may be even more significant.

This topic is closed to new replies.

Advertisement