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 6 1868
"Fred" <Fr**@somewhere .abc> wrote in message
news:cl******** **@sparta.btint ernet.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
"Fred" <Fr**@somewhere .abc> wrote in message
news:cl******** **@sparta.btint ernet.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
"Catalin Pitis" <ca***********@ iquestint.com.r enameme> 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.
"Fred" <Fr**@somewhere .abc> wrote in message
news:cl******** **@sparta.btint ernet.com... "Catalin Pitis" <ca***********@ iquestint.com.r enameme> 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
"Fred" <Fr**@somewhere .abc> wrote in message
news:cl******** **@sparta.btint ernet.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_BaseV ariable = 0;} // or you could write CBase() :
m_BaseVariable( 0){}
CBase(int n) : m_BaseVariable( n){}
};
class CDerived : public CBase
{
private:
int m_DerivedVariab le;
public:
CDerived() : m_nDerivedVaria ble(0) {} // CBase() is called automatically
before CDerived().
CDerived(int n, int m) : m_DerivedVariab le(n), CBase(m){}
};
"Chris Theis" <Ch*********@no spam.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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 age;
|
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 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?
|
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 want to use
std::string to do that so that I don't have to deal with all the hustle
of dealing with char *'s but as listed in the page (see link) below, it
is not advisable to use std::string in my exception classes.
The rational given is not...
|
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 "PyMethodDef"?
Now, onto my questions about the arguments:
I see that even when the Python function we are supplying takes no
arguments, (the argument flag is METH_NOARGS), that we still pass the
|
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 reference to v() defines the constructor, it will call v's
constructor, right ?
class vec
{
| |
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 the source code of Contact class >>
public class Contact : MembershipUser
|
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() { // blah }
public static ACIS Get_ACIS(string sACISCode) { // blah }
|
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 method:
static PyObject *
spam_system(PyObject *self, PyObject *args)
|
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;
};
|
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
| |
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |