Skip to main content
gamedev.net
🔒 Locked

Use initializer lists?

Started by michel Dec 29, 2003 at 5:06 AM 6 replies 700+ views
Original Post
michel
michel
The following is said in programming gems 2:
quote:
Consider the following class: class Vehicle { public: Vehicle(const std::string &name) // Don''t do this! { mName = name; } private: std:: string mName; }; Because member variables are constructed before the body of the constructor is invoked, this code calls the constructor for the string mName, and then calls the = operator to copy in the object''s name. What''s particularly bad about this example is that the default constructor for string may well allocate memory — in fact, more memory than may be necessary to hold the actual name assigned to the variable in the constructor for Vehicle. The following code is much better, and avoids the call to operator =. Further, given more information (in this case, the actual string to be stored), the nondefault string constructor can often be more efficient, and the compiler may be able to optimize away the Vehicle constructor invocation when the body is empty: class Vehicle { public: Vehicle(const std::string &name) : mName(name) {} private: std::string mName; };
What if I do more in the constructor then only assigning name to nName? What if I have more assignments to member variables? Should I still be following this tip? TIA, Michel
SiCrane
SiCrane
If you use a member initialization list you can still do things in the constructor body. However, to minimize the "double initializiation" overhead, you should use the member initialization list for as many member variables as possible.
michel
michel
I am also setting my member variables to null (false, 0) in the body of the constructor: Should this also not be done there?

This is what I am doing now:

Header:
M3DApplication(const std::string &pAppName);

Source:
M3DApplication::M3DApplication(const std::string &pAppName) : m_Name(pAppName)
{
m_hWnd = 0;
m_hInstance = 0;
ZeroMemory(&m_wClass, sizeof(WNDCLASS));
m_Name.clear();
m_Width = 0;
m_Height = 0;
m_BPP = 0;
m_Fullscreen = false;
m_Ready = false;
m_Running = false;
}

Any comments are welcome

TIA,
Michel



Cygon
Cygon
For plain built-in types such as ints, floats or pointers you won''t gain any performance by not using an initialization list, but just to improve consistency of your code, I would always use them for all attributes of your class.

They''re also the only way to pass arguments to constructors of stack-allocated attributes and base classes, so unless you''re dealing with a fixed size array or a C-style struct (where you could still provide a fake constructor in form of a function with arguments returning the filled struct) they''re the better way.

-Markus-
Professional C++ and .NET developer trying to break into indie game development.
Follow my progress: http://blog.nuclex-games.com/ or Twitter - Topics: Ogre3D, Blender, game architecture tips & code snippets.
michel
michel
So AP, you''re saying that I am really overdoing it?
And that I should go with whatever I feel happy with?
antareus
antareus
Just use initializer lists for initialization. Thats what they''re there for. If some logic is involved in setting values then you can always still set it to some sane value in the initializer list and maybe trim one assignment statement from the logic.
--God has paid us the intolerable compliment of loving us, in the deepest, most tragic, most inexorable sense.- C.S. Lewis

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.