Re: class object initialisation
"A" <A@iprimus.com. au> wrote in message news:<3f61b50b_ 1@news.iprimus. com.au>...[color=blue]
> Hi,
>
> I have always been taught to use an inialization list for initialising data
> members of a class. I realize that initialsizing primitives and pointers use
> an inialization list is exactly the same as an assignment, but for class
> types it has a different effect - it calls the copy constructor.
>
> My question is when to not use an initalisation list for initialising data
> members of a class?
>
>
> Regards
> Adi
>
>
>
> ---
> Outgoing mail is certified Virus Free.
> Checked by AVG anti-virus system (http://www.grisoft.com).
> Version: 6.0.516 / Virus Database: 313 - Release Date: 1/09/2003[/color]
You need to have an item in the initialize list if it's a constant or
a reference data member.
You also need to have the base class constructor in the initializer
list if the base class does not have a default contructor.
I recommend that you always TRY to use the initialize list.
An initializer list may not be appropriate when you have multiple
constructors, and serveral member variables that have common
initialization values with all the constructors.
In such cases, it's common to create an function initializer to
consolidate the logic.
Example:
class car
{
public:
car(){Initializ e();}
car(int x){Initialize() ;}
car(const char* s){Initialize() ;}
inline void Initialize()
{
QtyWheels = 4;
ID = -1;
Make = "n/a";
}
private:
int QtyWheels;
int ID;
std::string Make;
};
"A" <A@iprimus.com. au> wrote in message news:<3f61b50b_ 1@news.iprimus. com.au>...[color=blue]
> Hi,
>
> I have always been taught to use an inialization list for initialising data
> members of a class. I realize that initialsizing primitives and pointers use
> an inialization list is exactly the same as an assignment, but for class
> types it has a different effect - it calls the copy constructor.
>
> My question is when to not use an initalisation list for initialising data
> members of a class?
>
>
> Regards
> Adi
>
>
>
> ---
> Outgoing mail is certified Virus Free.
> Checked by AVG anti-virus system (http://www.grisoft.com).
> Version: 6.0.516 / Virus Database: 313 - Release Date: 1/09/2003[/color]
You need to have an item in the initialize list if it's a constant or
a reference data member.
You also need to have the base class constructor in the initializer
list if the base class does not have a default contructor.
I recommend that you always TRY to use the initialize list.
An initializer list may not be appropriate when you have multiple
constructors, and serveral member variables that have common
initialization values with all the constructors.
In such cases, it's common to create an function initializer to
consolidate the logic.
Example:
class car
{
public:
car(){Initializ e();}
car(int x){Initialize() ;}
car(const char* s){Initialize() ;}
inline void Initialize()
{
QtyWheels = 4;
ID = -1;
Make = "n/a";
}
private:
int QtyWheels;
int ID;
std::string Make;
};
Comment