This is a member initialization list. In the example you gave above, the class member variables int1, int2, and int3 would all be initialized with the value 0.
Basically in an initialization list you do just that: specify initial values for member variables.
Some Notes:
1. Class members are initialized in the order that they're declared in the class declaration. The order in which they're listed in the member initialization list doesn't matter. Thus, you might get burnt by doing something like this:
class CCustomer
{ public: // One-parameter constructor for when shipping address is the same as billing address
CCustomer( const CString& rsAddress );
The CCustomer constructor will crash because it tries to initialize m_sBillingAddress by copying m_sShippingAddress, but m_sShippingAddress hasn't been constructed yet.
2. In some cases, you must use a member initializer list. For instance, the following code doesn't compile:
class CCustomer
{ public: // One-parameter constructor for when shipping address is the same as billing address
CCustomer( const CString& rsAddress );
"const" member variables, as shown in this example, must be initialized in the member initialization list. Other times that you need to use a member initialization list are:
A. When the member is of a class type that has no default [that is, parameterless] constructor
B. When the member is a reference.
3. Member initialization lists can improve efficiency. Imagine that the code shown in the last example didn't declare the member variables as const. Thus it would compile. However, the compiler would generate two function calls for each CString: one to the CString default constructor and one to the assignment operator. By using a member initialization list, the compiler generates only a single call to the CString copy constructor.
One last note: to be totally technical, int1, int2, and int3 could be base classes, and not class members. In that case you would be passing the value 0 to three base class constructors. But I don't think that's what was intended by your example.
Hi,
See this a common method of initialising data variable of a class. KeithBrautigam has explained the stuff in very nice manner. Even then if u don't get the concept, look into some C++ documents and mannuals for more information on the same.
Regards,
Hemant Kapadia.
> Other times that you need to use a member initialization list are:
> A. When the member is of a class type that has no default
> [that is, parameterless] constructor
> B. When the member is a reference.
To be completely accurate, it is called the 'initialization list'. The difference is for a very good reason:
C. When the parent class has no default constructor
class foo{
CString _name;
public:
foo( CString name){
_name = name;
}
void showName(){ cout << _name << endl; }
};
class MyFoo : public foo{
public:
MyFoo() : foo("MyFoo"{}
};
class YourFoo ublic foo{
public:
YourFoo() : foo("YourFoo"{}
};
In this usage there is no 'member' that is being initialized in the list. The list is used to statisfy the parent class constructor parameters.
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.