473,394 Members | 1,800 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Extending a class - initialising new members.

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
6 1826

"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

"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

"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

"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
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

"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: claire.bell1 | last post by:
Hi, Im having problems initialising class member objects in the class's constructor e.g. My program isnt about monkeys, but there is too much code to post here. class monkey { private: int...
106
by: A | last post by:
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...
4
by: Divick | last post by:
Hi all, I want to subclass std::exception so as to designate the type of error that I want to throw, out of my classes, and for that I need to store the messages inside the exception classes. I...
3
by: Redefined Horizons | last post by:
I'm trying to understand the argument flags that are used in the method table of an extension module written in C. First let me ask this question about the method table. Is it an C array named...
10
by: imutate | last post by:
Some questions about ctors and class members Is v private in the following ? If it is why put the declaration at the top ? Is there any difference to putting it in the private section ? The...
3
by: Jeff | last post by:
Hey ASP.NET 2.0 I'm trying to extend the MembershipUser class, and have encounter a problem: << See in the middle of this post for info about why I do this >> << See below of this post for...
2
by: mark4asp | last post by:
Q: Initialising and updating a class with only static members & database dependency I have a class with the following members: public static List<ACISACIS_List; static AssetClass() { //...
3
by: dmoore | last post by:
Hi Folks: I have a question about the use of static members in Python/C extensions. Take the simple example from the "Extending and Embedding the Python Interpreter" docs: A simple module...
2
by: Ranganath | last post by:
Hi, Why is there a restriction that only integral types can be made static constant members of a class? For e.g., class B { private: static const double K = 10; };
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.