I have some code in Image.cpp:
Image::Image( int width, int height, int depth ) : m_sFileName(0)
{
...
}
and in Image.h:
class Image: public DrawAble, public RenderAble
{
...
private :
std::string *m_sFileName;
};
My question is: what is happening with m_sFilename
in the first line? I guess it is set to NULL but what's the point of doing it that way. Would it be the same to do:
Image::Image( int width, int height, int depth )
{
m_sFileName(0);
...
}
Those two variants are almost the same -- you're correct that the
is causing
m_sFileName
to be initialized to0
.The reason why C++ has this special initialization syntax becomes important when you want to create a
const Image
. (Probably not something you want to do in this case, but it can something you might want to do for "lightweight" types.) For aconst Image
,this
is a const pointer in the constructor as well as in every "normal" member function, and som_sFileName=0
is not allowed.To solve this problem, C++ has initialization lists, which perform initialization, not assignment. Incidentally, if
m_sFileName
were an object, there would be an additional difference besides theconst
considerations: The initialization list would causem_sFileName
's constructor to be called, whereas the assignment would call the assignment operator.Apart from all of these considerations, initialization lists are a good way to communicate intent -- to signify that you're initializing, not assigning.
The syntax you are using:
is called an initialization list. It will assign the value 0 to your member variable.
Using m_sFileName = 0; in the constructor body would be less performant because the member would be initialized twice (one time automatically because it is not included in the initialization list, and a second time with your explicit initialization).
in the constructor's body will be interpreted as calling a function with the name m_sFileName. You could replace it with
However, the recommended initialization is in the initialization list of the constructor, like in the first example. Any data member that is not initialized in the initialization list of the constructor will be automatically initialized with the default constructor of its type.
It would be the same as doing
Please note that using a pointer to a
std::string
is usually not a good idea, as an empty string is an equally good nothing-here-marker and you do not have to care about the destruction if you make it a normal member.It does the same as:
The first uses what's called an initialization list.
When you enter the body of the constructor, all of the classes members must have been constructed (so they can be used). So if you have this:
So,
str
gets constructed, then assigned. Better would have been:So that
str
gets directly constructed. This does not make a difference in your case because pointers have no constructor.It is generally considered good practice to use an initialization list over running code in the constructor. The initialization list should be used for initializing, the constructor should be used for running code.
Also, why use a pointer to string? If you want a string, use a string; not a pointer to string. Chances are, you actually want a string.
More about initializer lists:
Initializer lists have more uses than just initializing members of the class. They can be used to pass arguments into base constructors:
Or constant members:
Or references: