473,545 Members | 2,085 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Maybe a challenging problem on inheritance

Dear All,

I have a libaray which provides a base class of name Base and several
derived classes of name Derived1, Derived2 and so on.

I want to add a client data into those classes, such as

class MyBase: public Base
{
ClientDataType ClientData;
};

class MyDerived1: public Derived1
{
ClientDataType ClientData;
};

class MyDerived2: public Derived2
{
ClientDataType ClientData;
};

Howeven, I still want MyDerived1, MyDerived2 is derived from MyBase and
I don't want to change the provided library. My code cannot realize
it.Anyone can give me some suggestion.

I appreicate your kind help.

Shuisheng

Dec 18 '06 #1
8 1703
shuisheng wrote:
Dear All,

I have a libaray which provides a base class of name Base and several
derived classes of name Derived1, Derived2 and so on.

I want to add a client data into those classes, such as

class MyBase: public Base
{
ClientDataType ClientData;
};

class MyDerived1: public Derived1
{
ClientDataType ClientData;
};

class MyDerived2: public Derived2
{
ClientDataType ClientData;
};

Howeven, I still want MyDerived1, MyDerived2 is derived from MyBase and
I don't want to change the provided library. My code cannot realize
it.Anyone can give me some suggestion.

I appreicate your kind help.

Shuisheng
template <typename T>
class DerivedWithClie ntData : public T
{
ClientDataType clientData;
}

class MyBase : public DerivedWithClie ntData<Base>
{
// etc
};

class MyDerived1 : public DerivedWithClie ntData<Derived1 >
{
// etc
};

class MyDerived2 : public DerivedWithClie ntData<Derived2 >
{
// etc
};
Dec 18 '06 #2
shuisheng napsal:
Dear All,

I have a libaray which provides a base class of name Base and several
derived classes of name Derived1, Derived2 and so on.

I want to add a client data into those classes, such as

class MyBase: public Base
{
ClientDataType ClientData;
};

class MyDerived1: public Derived1
{
ClientDataType ClientData;
};

class MyDerived2: public Derived2
{
ClientDataType ClientData;
};

Howeven, I still want MyDerived1, MyDerived2 is derived from MyBase and
I don't want to change the provided library. My code cannot realize
it.Anyone can give me some suggestion.

I appreicate your kind help.

Shuisheng
You can derive class from 2 base classes. When they have some common
parent, it should be virtual inheritance:

class Base
{
public:
int bdata_;
};

class Derived1: public virtual Base
{
public:
int data1_;
};

class MyBase: public virtual Base
{
public:
double mbdata_;
};

class MyDerived1: public Derived1, public MyBase
{
public:
double xyz_;
};

int main()
{
MyDerived1 md1;
md1.bdata_ = 5; // Without virtual inheritance it would be
ambigous, because Base class would be present twice
}

But read
http://www.parashift.com/c++-faq-lit....html#faq-25.8
for virtual inheritance.

Dec 18 '06 #3

red floyd wrote:
shuisheng wrote:
Dear All,

I have a libaray which provides a base class of name Base and several
derived classes of name Derived1, Derived2 and so on.

I want to add a client data into those classes, such as

class MyBase: public Base
{
ClientDataType ClientData;
};

class MyDerived1: public Derived1
{
ClientDataType ClientData;
};

class MyDerived2: public Derived2
{
ClientDataType ClientData;
};

Howeven, I still want MyDerived1, MyDerived2 is derived from MyBase and
I don't want to change the provided library. My code cannot realize
it.Anyone can give me some suggestion.

I appreicate your kind help.

Shuisheng

template <typename T>
class DerivedWithClie ntData : public T
{
ClientDataType clientData;
}

class MyBase : public DerivedWithClie ntData<Base>
{
// etc
};

class MyDerived1 : public DerivedWithClie ntData<Derived1 >
{
// etc
};

class MyDerived2 : public DerivedWithClie ntData<Derived2 >
{
// etc
};
I tested your method. But it seems to do not work.

MyBase = new MyDerived1(); // give some errors.

so MyDerived is not derived from MyBase. If I misunderstand you, please
let me know.

Thanks!

Shuisheng

Dec 18 '06 #4

Ondra Holub wrote:
shuisheng napsal:
Dear All,

I have a libaray which provides a base class of name Base and several
derived classes of name Derived1, Derived2 and so on.

I want to add a client data into those classes, such as

class MyBase: public Base
{
ClientDataType ClientData;
};

class MyDerived1: public Derived1
{
ClientDataType ClientData;
};

class MyDerived2: public Derived2
{
ClientDataType ClientData;
};

Howeven, I still want MyDerived1, MyDerived2 is derived from MyBase and
I don't want to change the provided library. My code cannot realize
it.Anyone can give me some suggestion.

I appreicate your kind help.

Shuisheng

You can derive class from 2 base classes. When they have some common
parent, it should be virtual inheritance:

class Base
{
public:
int bdata_;
};

class Derived1: public virtual Base
{
public:
int data1_;
};
Derived1 is defined by the provided library. So I do not want to change
it to be 'virtual' derived.

Thanks.

Shuisheng

Dec 18 '06 #5

shuisheng wrote:
Dear All,

I have a libaray which provides a base class of name Base and several
derived classes of name Derived1, Derived2 and so on.

I want to add a client data into those classes, such as

class MyBase: public Base
{
ClientDataType ClientData;
};

class MyDerived1: public Derived1
{
ClientDataType ClientData;
};

class MyDerived2: public Derived2
{
ClientDataType ClientData;
};

Howeven, I still want MyDerived1, MyDerived2 is derived from MyBase and
I don't want to change the provided library. My code cannot realize
it.Anyone can give me some suggestion.
You might consider the Decorator pattern:

http://en.wikipedia.org/wiki/Decorator_pattern

You might also make your decorator class inherit from the base, so you
can use it anywhere the other classes are expected.

--mpa

Dec 18 '06 #6

shuisheng wrote:
Derived1 is defined by the provided library. So I do not want to change
it to be 'virtual' derived.
It is absolutely not clear what do you want to do with the help of
inheritance (as design way). You can do like with

Base -Derived1, Derived2, Derived3 - you library classes inheritence
Base -Mybase - you base class improving
interface Base for you
Mybase -MyDerived1, MyDerived2, MyDerived3 - you improved classes
inheritence

class MyDerived1: public Mybase
{
protected:
//can be placed in Mybase
Base *library;

//Base intarface
public:
method(){ library->method(); }

//Mybase intarface
public:
new_method(){ }//not exist in Base

public:
MyDerived1()thr ow(exception&): library(0){ library=new Derived1; }
//lib must be allocated with "new"
MyDerived1(Base & lib)throw():lib rary(&lib){}
//MyDerived1(cons t MyDerived1&)
//MyDerived1& operator= (const MyDerived1&)
[virtual] ~MyDerived1()th row(){ delete library; library=0; }
};

Instead of composition or aggregation of "Base*" you can use multiple
private inheritance of concrete Base derived, but the last case is bad
design way. It maybe not C++ language question, for C++ do not set
limits to classes design.

Dec 19 '06 #7

Grizlyk wrote:
It maybe not C++ language question, for C++ do not set
limits to classes design.
I decided to add. To understand design of classes you need know:

1. "what is structured programs" - not object-oriented (C-style)
programs.

2. "how do 'job -structured program' decomposition" - design ways for
not object-oriented (C-style) programs (function, structures, modules).
3. "what is ATD (abstract data types)"

4. "what is object-oriented programs" - to understand encapsulation,
polymorphism and inheritance paradigm, the goal of inheritance as ATD
design way.

5. "how do 'job -classes' decomposition". There is popular design way
of classes as "design patterns" (bridge, decorator etc).

Sorry, I do no know good english links for 1-5. Maybe to first overview
the questions can be used:
For 3,4 - ancient (1986) "B. Stroustrup: What is Object-Oriented
Programming?"
For 5 -
http://en.wikipedia.org/wiki/Design_...ter_science%29

Dec 19 '06 #8

Grizlyk wrote:
Maybe to first overview the questions can be used:
For 5 -
http://en.wikipedia.org/wiki/Design_...ter_science%29
For 5, they are say, there is "easy to understand" book:
A. Shalloway, J. Trott
Design Pattern Explained
Addison-Wesley, 2002
www.netobjectives.com/dpexplained

PS: Do not try to find answers in reverse order from 5 to 1

Dec 26 '06 #9

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

Similar topics

2
4363
by: AIM | last post by:
Error in msvc in building inheritance.obj to build hello.pyd Hello, I am trying to build the boost 1.31.0 sample extension hello.cpp. I can not compile the file inheritance.cpp because the two files containing some templates: adjacency_list.hpp and mem_fn.hpp can not compile. Does anyone have any solutions?
2
4323
by: Graham Banks | last post by:
Does using multiple inheritance introduce any more performance overhead than single inheritance?
4
2887
by: JKop | last post by:
I'm starting to think that whenever you derive one class from another, that you should use virtual inheritance *all* the time, unless you have an explicit reason not to. I'm even thinking that there shouldn't have been a "virtual" keyword for this purpose, but instead, a "nonvirtual" keyword! In teaching inheritance, you see the common...
5
2169
by: Morgan Cheng | last post by:
It seems no pattern defined by GoF takes advantage of multiple inheritance. I am wondering if there is a situation where multiple inheritance is a necessary solution. When coding in C++, should multiple inheritance still be avoided? If yes, why multiple inheritance is introducted into C++?
22
23323
by: Matthew Louden | last post by:
I want to know why C# doesnt support multiple inheritance? But why we can inherit multiple interfaces instead? I know this is the rule, but I dont understand why. Can anyone give me some concrete examples?
11
1406
by: George | last post by:
Was my question too challenging for everyone? I thought I would get a much quicker response, complete with witty and (sometimes) condescending remarks Here's my question again, in case you missed it How do I reload a page Notice I did not ask "How do I *refresh* a page". I have no interest in the header meta refresh as this clears out the...
18
1556
by: Frankie | last post by:
I have been hired to go to a former client of mine and train their staff programmers on ASP.NET. These guys have only Mainframe, MS Access, SQL Server, and VB6 desktop application development experience (with absolutely no HTML or Web application experience). Before jumping into any code I think it's important to get them to understand the...
5
1223
by: alainpoint | last post by:
Hi, I have what in my eyes seems a challenging problem. Thanks to Peter Otten, i got the following code to work. It is a sort of named tuple. from operator import itemgetter def constgetter(value): def get(self): return value return get def createTuple(*names):
60
4875
by: Shawnk | last post by:
Some Sr. colleges and I have had an on going discussion relative to when and if C# will ever support 'true' multiple inheritance. Relevant to this, I wanted to query the C# community (the 'target' programming community herein) to get some community input and verify (or not) the following two statements. Few programmers (3 to7%)...
0
7467
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7656
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7807
jinu1996
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...
0
7756
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5971
agi2029
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...
0
4944
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...
0
3450
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...
0
3442
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
703
bsmnconsultancy
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...

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.