Extending a class - initialising new members.

F

Fred

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
 
C

Catalin Pitis

Fred said:
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
 
C

Chris Theis

Fred said:
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
 
F

Fred

Catalin Pitis said:
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.
 
C

Catalin Pitis

Fred said:
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
 
P

PKH

Fred said:
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){}
};
 
F

Fred

Chris Theis said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top