By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,089 Members | 1,340 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,089 IT Pros & Developers. It's quick & easy.

Extending a class - initialising new members.

P: n/a
Hi

I have a class defined in a library that I'd like to add some extra
functionality to. This will involve adding a few member variables and a few
related methods. As I understand it I can simply inherit from the existing
class and add in my new variables and methods - but what about
initialisation?

Let's say I add new member variables a and b which I want to always have
initial values of 0.0. Do I have to override all the constructors of the
base class in order to implement this initialisation or is there some way of
doing this implicitly?

Many Thanks
Fred
Jul 22 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a

"Fred" <Fr**@somewhere.abc> wrote in message
news:cl**********@sparta.btinternet.com...
Hi

I have a class defined in a library that I'd like to add some extra
functionality to. This will involve adding a few member variables and a
few
related methods. As I understand it I can simply inherit from the existing
class and add in my new variables and methods - but what about
initialisation?

Let's say I add new member variables a and b which I want to always have
initial values of 0.0. Do I have to override all the constructors of the
base class in order to implement this initialisation or is there some way
of
doing this implicitly?

Many Thanks
Fred


Unfortunately, the constructors are not inherited. So, if you want to keep
the same constructors for the derived class, you should declare them in the
derived clas with the same interface as in the base class and call the base
class constructors with the specified parameters. Unfortunately, it means a
lot of (unnecessary) code to be written. Fortunately, from the performance
point of view, these calls can be optimized by the compiler.

However, you should think: Are all the base class constructors really
necessary in the derived class?

Br/
Catalin
Jul 22 '05 #2

P: n/a

"Fred" <Fr**@somewhere.abc> wrote in message
news:cl**********@sparta.btinternet.com...
Hi

I have a class defined in a library that I'd like to add some extra
functionality to. This will involve adding a few member variables and a few related methods. As I understand it I can simply inherit from the existing
class and add in my new variables and methods - but what about
initialisation?

Let's say I add new member variables a and b which I want to always have
initial values of 0.0. Do I have to override all the constructors of the
base class in order to implement this initialisation or is there some way of doing this implicitly?

Many Thanks
Fred


Only default ctors of the base class are automatically called by the ctor of
the derived class. If you have parameterized ctors then youŽll have to
provide their interface in the derived class and explicitly call the base
version.

Cheers
Chris

Jul 22 '05 #3

P: n/a

"Catalin Pitis" <ca***********@iquestint.com.renameme> wrote in message
news:2u*************@uni-berlin.de...

Unfortunately, the constructors are not inherited. So, if you want to keep
the same constructors for the derived class, you should declare them in the derived clas with the same interface as in the base class and call the base class constructors with the specified parameters. Unfortunately, it means a lot of (unnecessary) code to be written. Fortunately, from the performance
point of view, these calls can be optimized by the compiler.

However, you should think: Are all the base class constructors really
necessary in the derived class?

Thanks for that.

In my particular case there are about 6-7 different constuctors and I've
explicitly reproduced these in my derived class.

In reality, I believe only 1 or 2 of these constructors will be used with my
new class, however it seems safer to assume they all could be used since the
object I'm modifiying is used in a lot of places and is subject to change by
other coders. If I did limit my new constructors to those I believed
necessary and I missed one (or an unmodified one was subsequently used) then
I run the risk of having uninitialised data used?

It seems surprising that c++ doesn't provide a mechanism for a global
initialisation of member variables or at least some compiler warnings when
these are not initialised (I'm using MS Visual Studio)

Thanks again.
Jul 22 '05 #4

P: n/a

"Fred" <Fr**@somewhere.abc> wrote in message
news:cl**********@sparta.btinternet.com...

"Catalin Pitis" <ca***********@iquestint.com.renameme> wrote in message
news:2u*************@uni-berlin.de...

Unfortunately, the constructors are not inherited. So, if you want to
keep
the same constructors for the derived class, you should declare them in the
derived clas with the same interface as in the base class and call the

base
class constructors with the specified parameters. Unfortunately, it means

a
lot of (unnecessary) code to be written. Fortunately, from the
performance
point of view, these calls can be optimized by the compiler.

However, you should think: Are all the base class constructors really
necessary in the derived class?

Thanks for that.

In my particular case there are about 6-7 different constuctors and I've
explicitly reproduced these in my derived class.

In reality, I believe only 1 or 2 of these constructors will be used with
my
new class, however it seems safer to assume they all could be used since
the
object I'm modifiying is used in a lot of places and is subject to change
by
other coders. If I did limit my new constructors to those I believed
necessary and I missed one (or an unmodified one was subsequently used)
then
I run the risk of having uninitialised data used?


If the class is written well then all the members should be initialized in
constructors. You have the initialization list, from where you can specify
which of the base class constructor to be called and what constructors to be
used for the each data member. For example

class A
{
public:
A(): x( 10) { }

private:
int x;
}

The constructor calls the copy constructor of type int, to copy the value 10
to data member x.

If you miss a data member from the initialization list, the default
constructor for that data member will automatically be called (if the class
of the data member has no default constructor, then you'll get an error).

The problem is with the built in types, for which the default constructors
don't necessarely initialize with a specific value. For example, in the code
above, if the constructor was like:

A() { }

the default constructor of type int (built in type) doesn't initialize the
value of x with 0. At least it is not specified in standard.

Therefore, a good policy to be followed is to explictly initialize the
built-in typed data members in the constructors of the class. (this includes
pointers)

It seems surprising that c++ doesn't provide a mechanism for a global
initialisation of member variables or at least some compiler warnings when
these are not initialised (I'm using MS Visual Studio)


I think is hard to figure out this: from compiler's point of view, as I
said, a default constructor is called for each data member that doesn't
exist in the initialization list of the called constructor.

Br/
Catalin
Jul 22 '05 #5

P: n/a
PKH

"Fred" <Fr**@somewhere.abc> wrote in message
news:cl**********@sparta.btinternet.com...
Hi

I have a class defined in a library that I'd like to add some extra
functionality to. This will involve adding a few member variables and a
few
related methods. As I understand it I can simply inherit from the existing
class and add in my new variables and methods - but what about
initialisation?

Let's say I add new member variables a and b which I want to always have
initial values of 0.0. Do I have to override all the constructors of the
base class in order to implement this initialisation or is there some way
of
doing this implicitly?

Many Thanks
Fred


You can do it like this:

class CBase
{
private:
int m_BaseVariable;

public:
// 2 examples of constructors
CBase(){m_BaseVariable = 0;} // or you could write CBase() :
m_BaseVariable(0){}
CBase(int n) : m_BaseVariable(n){}
};

class CDerived : public CBase
{
private:
int m_DerivedVariable;

public:
CDerived() : m_nDerivedVariable(0) {} // CBase() is called automatically
before CDerived().
CDerived(int n, int m) : m_DerivedVariable(n), CBase(m){}
};

Jul 22 '05 #6

P: n/a

"Chris Theis" <Ch*********@nospam.cern.ch> wrote in message
news:cl**********@sunnews.cern.ch...
Only default ctors of the base class are automatically called by the ctor of the derived class. If you have parameterized ctors then youŽll have to
provide their interface in the derived class and explicitly call the base
version.

Cheers
Chris


As I suspected.

Thanks for that.
Jul 22 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.