By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,136 Members | 1,021 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,136 IT Pros & Developers. It's quick & easy.

Maybe a challenging problem on inheritance

P: n/a
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
Share this Question
Share on Google+
8 Replies


P: n/a
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 DerivedWithClientData : public T
{
ClientDataType clientData;
}

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

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

class MyDerived2 : public DerivedWithClientData<Derived2>
{
// etc
};
Dec 18 '06 #2

P: n/a
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

P: n/a

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 DerivedWithClientData : public T
{
ClientDataType clientData;
}

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

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

class MyDerived2 : public DerivedWithClientData<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

P: n/a

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

P: n/a

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

P: n/a

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()throw(exception&):library(0){ library=new Derived1; }
//lib must be allocated with "new"
MyDerived1(Base& lib)throw():library(&lib){}
//MyDerived1(const MyDerived1&)
//MyDerived1& operator= (const MyDerived1&)
[virtual] ~MyDerived1()throw(){ 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

P: n/a

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

P: n/a

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 discussion thread is closed

Replies have been disabled for this discussion.