Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need Explanation of Code. 2

Status
Not open for further replies.

RSTR

Programmer
Mar 25, 2000
27
US
I have seen this written in a class before:

constructor(): int1(0), int2(0), int3(0), etc(0) {}

could someone explain the part starting with the colon?

-RSTR

benthecat@hotmail.com

 
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 );

CString m_sBillingAddress;
CString m_sShippingAddress;
};

CCustomer::CCustomer( const CString& rsAddress ) :
m_sShippingAddress( rsAddress ),
m_sBillingAddress( m_sShippingAddress )
{
}

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 CString m_sBillingAddress;
const CString m_sShippingAddress;
};

CCustomer::CCustomer( const CString& rsAddress ) :
{
m_sBillingAddress = rsAddress;
m_sShippingAddress = 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.
 
Wow, that was beautiful.. thankyou.

- RSTR benthecat@hotmail.com

 
> 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(&quot;MyFoo&quot;){}
};

class YourFoo :public foo{
public:
YourFoo() : foo(&quot;YourFoo&quot;){}
};


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.

-pete

 
Good point; I didn't think of the case when the base class has no default constructor. Thanks.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top