Maybe a challenging problem on inheritance

S

shuisheng

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
 
R

red floyd

shuisheng said:
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
};
 
O

Ondra Holub

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-lite/multiple-inheritance.html#faq-25.8
for virtual inheritance.
 
S

shuisheng

red said:
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
 
S

shuisheng

Ondra said:
shuisheng napsal:

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
 
M

Michael Ashton

shuisheng said:
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
 
G

Grizlyk

shuisheng said:
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.
 
G

Grizlyk

Grizlyk said:
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_pattern_(computer_science)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top