473,387 Members | 1,899 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,387 software developers and data experts.

Inherit member variables?

I'm having a little difficultly with inheritance.
I want to have a class "SubT" inherit from class "T".
SubT contains all the information in T, plus a little more based upon
the member variables of T.

For efficiency, I cannot merely have a T object in SubT.
What I'd like is something as follows:
class T {
T(int a) : _a(a) {}
protected:
int _a;
};

class SubT : public T {
SubT(int a) : _a(a), _b(a+a) {}
SubT(T foo) : _a(foo._a), _b(foo._a+foo._a) {}
public:
int _b;
};

Unfortunately, SubT doesn't seem to contain member variable _a. Why
not?
Could someone suggest an efficient way to write the constructors for
SubT?
[Assume that SubT needs to contain both _a, and _b, and that you can't
optimize one out.]

Joseph

Nov 26 '05 #1
1 1742
Joseph Turian wrote:
I'm having a little difficultly with inheritance.
I want to have a class "SubT" inherit from class "T".
SubT contains all the information in T, plus a little more based upon
the member variables of T.

For efficiency, I cannot merely have a T object in SubT.
What I'd like is something as follows:
class T {
T(int a) : _a(a) {}
protected:
int _a;
};

class SubT : public T {
SubT(int a) : _a(a), _b(a+a) {}
SubT(T foo) : _a(foo._a), _b(foo._a+foo._a) {}
public:
int _b;
};

Unfortunately, SubT doesn't seem to contain member variable _a. Why
not?
It does, but not directly, it contains T, which contains a.

Could someone suggest an efficient way to write the constructors for
SubT?
You should use the T cosntructor, like this

SubT(int a) : T(a), _b(a+a) {}
SubT(T foo) : T(foo._a), _b(foo._a+foo._a) {}
[Assume that SubT needs to contain both _a, and _b, and that you can't
optimize one out.]

Joseph


john
Nov 26 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: nikolai.onken | last post by:
Hello, I got following problem trying to inherit a variable from another class class Core { public $obj; function initialize($obj) {
5
by: Ernst Murnleitner | last post by:
Hello, is it possible to derive from std::vector and derive also its iterator? If I do it like in the example below, I get a problem when I need the begin of the vector: begin() returns the...
17
by: lm401 | last post by:
I'm trying to work with the following idea: class animal: def __init__(self, weight, colour): self.weight = weight self.colour = colour class bird(animal): def __init__(self, wingspan):
21
by: T.A. | last post by:
I understand why it is not safe to inherit from STL containers, but I have found (in SGI STL documentation) that for example bidirectional_iterator class can be used to create your own iterator...
7
by: Valeriu Catina | last post by:
Hi, consider the Shape class from the FAQ: class Shape{ public: Shape(); virtual ~Shape(); virtual void draw() = 0;
13
by: learning | last post by:
Hi I have a static class written by other team which encapsulates a database instance. but I need to extend it to incldue other things. I know that C# static class is sealed and can;t be inherited...
5
by: Harinezumi | last post by:
Hello, I have a base class Base with given member variables, getters and setters, but it doesn't do much on its own. Then there are functionalities (Func) which can use this base class's member...
4
by: nw | last post by:
Hi All, I currently have a vector of objects (lets call them MyObject). I want to perform various operations regularly on the whole vector, for example find the maximum, average, or operations...
7
by: Immortal Nephi | last post by:
My project grows large when I put too many member functions into one class. The header file and source code file will have approximately 50,000 lines when one class contains thousand member...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...

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.