class Exception { public: Exception(const wchar_t* szwText) : m_wstrText(szwText) // ВОТ В ЭТОЙ СТРОКЕ {;}; // дальнейшие определения }
|
2 answers
Initialization of fields in the constructor. That is, formally, understand it so
class Exception { public: Exception(const wchar_t* szwText) { m_wstrText = szwText } //дальнейшие определения };
Only in the first case can the compiler make the code more efficient.
standard defined in clause 12.6.2
- <tediousness> only in the first case the copy constructor is called, and in the second, the default constructor and the assignment operator are invoked . </ tediousness> - VladD
- onebut can optimize. and call the correct constructor right away. - KoVadim pm
- @KoVadim: Yeah, has the right, even if different constructors have different side effects - VladD
- But the funny thing is that the implementation of other designers, who in fact does not use, will require, but the standard requires :) - KoVadim
- Yeah, so that no one knows what type of optimization worked or not :) - VladD
|
To the answer @KoVadim I will add that there are cases when such a construction is not only desirable, but also necessary
- Calling the base class constructor
- Initiating constant
- Initiation of the link (not a pointer!)
Perhaps something forgot. If anyone specifies other cases, I will be grateful.
PS Here's another: the included object needs to be initiated by the constructor with parameters, and it has no assignment operator.
- oneInitialization of the included object, which has no constructor without parameters, seems to be also impossible without an initialization list. - nnesterov
- Yes, that's right. - skegg
|