const , and reference variables must be initialized in the declared string.
class Something { private: const int m_nValue; public: Something() { m_nValue = 5; } };
will generate code equivalent to:
const int nValue;
Assigning values ββto const variables or reference elements in the constructor body is not enough.
C ++ provides another way to initialize member variables, which allows you to initialize member variables when they are created, not later. This is done using an initialization list.
You can assign values ββto variables in two ways: explicitly and implicitly: view a plain copy in clipboardprint?
int nValue = 5;
Using an initialization list is very similar to doing implicit assignments.
Remember that the member initialization list used to initialize the underlying and data object objects is in the definition, not the constructor declaration.
Learn more about cpp-tutorial and Code Wrangler .
VonC Dec 10 '08 at 6:57 2008-12-10 06:57
source share