Advertisement

C++ Syntax : Constructor particularity

Started by December 29, 2002 09:46 AM
3 comments, last by _Danneman_ 21 years, 10 months ago
Heh, feels like Im taking advantage of the expertise here Anyway, this is about constructors. This one has an extra part to it that I dont understand: ----------------------------- SmyStruct::SmyStruct(int NumInputs):m_NumInputs(NumInputs){*code goes here*} ----------------------------- So, the unfamiliar part to constructors comes with the single semi-colon. What exactly am I doing with the ''...:m_NumInputs(NumInputs)'' part?
------------------------Why of course the people don’t want war. ... That is understood. But after all it is the leaders of the country who determine the policy, and it is always a simple matter to drag the people along, whether it is a democracy, or a fascist dictatorship, or a parliament, or a communist dictatorship ...Voice or no voice, the people can always be brought to the bidding of the leaders. That is easy. All you have to do is to tell them they are being attacked, and denounce the pacifists for lack of patriotism and exposing the country to danger. It works the same in any country. [Herman Goering]
That''s the constructor initializer list, used in the contructor of a class using inheritance. It is used to initialize the base object, in this case m_NumInputs, with proper parameters.

Advertisement

MyStruct(int NumInputs)
: m_NumInputs(NumInputs)
{
}

is (almost) the same as:

MyStruct(int NumInputs)
{
m_NumInputs = NumInputs;
}


Update GameDev.net system time campaign: ''date ddmmHHMMYYYY''
Arguing on the internet is like running in the Special Olympics: Even if you win, you're still retarded.[How To Ask Questions|STL Programmer's Guide|Bjarne FAQ|C++ FAQ Lite|C++ Reference|MSDN]
Not only is it used to initialise the (possible) base class, it is also used to initialise member variables by directly calling their constructors. If you assign to them in the body of the constructor, they will actually first be default-constructed THEN assigned to, which is not only inefficient (you essentially initialise the variable twice), but may be impossible if the member variable is a constant, a reference, or doesn''t have a default constructor.

Initializer lists are your friends.

[ Start Here ! | How To Ask Smart Questions | Recommended C++ Books | C++ FAQ Lite | Function Ptrs | CppTips Archive ]
[ Header Files | File Format Docs | LNK2001 | C++ STL Doc | STLPort | Free C++ IDE | Boost C++ Lib | MSVC6 Lib Fixes ]
"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
Great, I got a grip on it now Thaks!!
------------------------Why of course the people don’t want war. ... That is understood. But after all it is the leaders of the country who determine the policy, and it is always a simple matter to drag the people along, whether it is a democracy, or a fascist dictatorship, or a parliament, or a communist dictatorship ...Voice or no voice, the people can always be brought to the bidding of the leaders. That is easy. All you have to do is to tell them they are being attacked, and denounce the pacifists for lack of patriotism and exposing the country to danger. It works the same in any country. [Herman Goering]

This topic is closed to new replies.

Advertisement