Dynamic Inheritance Query..


P

praveenraj1987

I was just going through my text of object oriented analysis and
design in which it mentioned about dynamic inheritance.

i have a doubt that does C++ supports dynamic inheritance or is there
any other way to achieve it.
and does below code snippet show dynamic inheritance?.. i don't think
so but though type of derived is not fixed.

#include<iostream>
#include<typeinfo>
using namespace std;

class Base1
{
protected:
int val1;

public:
Base1(int a):val1(a)
{
cout<<endl<<"Constructing BASE1";
}

void gettype()
{
cout<<endl<<"BASE1"<<endl;
}


};

class Base2
{
protected:
int val1;

public:
Base2(int a) : val1(a)
{
cout<<endl<<"Constructing BASE2";
}


void gettype()
{
cout<<endl<<"BASE2"<<endl;
}

};

template<typename T>
class Derived : public T
{
public:

int val2;

Derived(int b,int a):T(a),val2(b)
{
cout<<endl<<"Constructing DERIVED";
}

void display()
{
cout<<endl<<"Type Inherited is";
T::gettype();
cout<<val2<<endl<<T::val1<<endl;
cout<<endl<<"type ID = "<<typeid(Derived).name()<<endl; //gives type
info
}

};

int main()
{
Derived<Base1> obj1(1,2);//inherit Base1
obj1.display();

Derived<Base2> obj2(3,4);//inherit Base2
obj2.display();
}

Regards..
 
Ad

Advertisements

A

alfps

I was just going through my text of object oriented analysis and
design in which it mentioned about dynamic inheritance.

i have a doubt that does C++ supports dynamic inheritance or is there
any other way to achieve it.
and does below code snippet show dynamic inheritance?.. i don't think
so but though type of derived is not fixed.

Define "dynamic inheritance". There's your answer. Whatever it is.


Cheers & hth.,

- Alf
 
P

praveenraj1987

Define "dynamic inheritance". There's your answer. Whatever it is.

Cheers & hth.,

- Alf

Dynamic inheritance is to change the base class of a type
dynamically.. simula nd smalltalk supports it.. i googled abt it and
it's written in some papers that they have used/implemented dynamic
inheritance. i wanted to know whether C++ supports it and if it does,
can we achieve a runtime change in inheritance level.. and how to
achieve that..

i tried with the above code to inherit different classes.. can this be
called as dynamic inheritance...
 
P

praveenraj1987

(e-mail address removed) kirjutas:





Inheritance is compile-time concept in C++, so you can't change it at
runtime.

What you can do is to add another level of indirection: put a pointer to
the "base" class object inside your class, and add wrapper functions to
forward everything to the "base" class virtual functions. Now you can
change the pointer to an object of some other class (derived from "base"
class) at runtime. I'm sure this technique has been named as some kind
of pattern/idiom, but I'm not sure what exactly.

Paavo

even i read about this to put a wrapper function and and adding a base
pointer..
if some one could help me with a small code snippet to get hold of it..
 
M

Michael DOUBEZ

(e-mail address removed) a écrit :
On 23 Des, 01:17, (e-mail address removed) wrote:
I was just going through my text of object oriented analysis and
design in which it mentioned about dynamic inheritance.
i have a doubt that does C++ supports dynamic inheritance or is
there any other way to achieve it. [snip]
Dynamic inheritance is to change the base class of a type
dynamically..[snip]

What you can do is to add another level of indirection: put a pointer to
the "base" class object inside your class, and add wrapper functions to
forward everything to the "base" class virtual functions. Now you can
change the pointer to an object of some other class (derived from "base"
class) at runtime. I'm sure this technique has been named as some kind
of pattern/idiom, but I'm not sure what exactly.

It is dynamic polymorphism. Concerning the pattern, IMO it is kin to the
Envelope/Letter pattern.

Note that it does not achieve dynamic typing (hence dynamic inheritance)
unless you implement a dispatching system. Base must have a common
interface.

even i read about this to put a wrapper function and and adding a base
pointer..
if some one could help me with a small code snippet to get hold of it..

Retaking your example

struct base_interface
{
virtual void gettype()const=0;

virtual void set_val(int val)=0;
virtual int get_val()const=0;
};

class Base1: public base_interface
{
public:
Base1(int a):val1(a){
cout<<"Constructing BASE1"<<endl;
}
virtual void gettype()const {
cout<<"BASE1";
}
virtual void set_val(int val){
val1=val;
}
virtual int get_val()const{
return val1;
}
protected: int val1;
};

class Base2: public base_interface
{
//same as Base1 with val2
};

class Derived: base_interface
{
public: int val2;

Derived(int b,base_interface* b):val2(b),dynbase(b)
{ //base not null - could use default base
assert(b);
}

//set base
base_interface* base(base_interface* b)
{ assert(b);
base_interface* const tmp=dynbase;
dynbase=b;
return tmp;
}

virtual void gettype()const {
cout<<"Derived inheriting from ";
dynbase->gettype();
}

//delegating to base
void set_val(int val)
{ assert(dynbase);
dynbase->set_val(val);
}
int get_val()const
{ assert(dynbase);
return dynbase->get_val();
}
};

void display(base_interface& d)
{
cout<<"Type is"<<d->gettype()<<endl;
//...
}

int main()
{
Base1 b1(2);
display(b1);

Base2 b2(4);
display(b2);

Derived obj(1,&b1);
display(obj);

obj.base(&b2);
display(obj);
}

I have not compiled the code but it should get you started
 
P

praveenraj1987

(e-mail address removed) a écrit :


On 23 Des, 01:17, (e-mail address removed) wrote:
I was just going through my text of object oriented analysis and
design in which it mentioned about dynamic inheritance.
i have a doubt that does C++ supports dynamic inheritance or is
there any other way to achieve it. [snip]
Dynamic inheritance is to change the base class of a type
dynamically..[snip]
What you can do is to add another level of indirection: put a pointer to
the "base" class object inside your class, and add wrapper functions to
forward everything to the "base" class virtual functions. Now you can
change the pointer to an object of some other class (derived from "base"
class) at runtime. I'm sure this technique has been named as some kind
of pattern/idiom, but I'm not sure what exactly.

It is dynamic polymorphism. Concerning the pattern, IMO it is kin to the
  Envelope/Letter pattern.

Note that it does not achieve dynamic typing (hence dynamic inheritance)
unless you implement a dispatching system. Base must have a common
interface.
even i read about this to put a wrapper function and and adding a base
pointer..
if some one could help me with a small code snippet to get hold of it..

Retaking your example

struct base_interface
{
   virtual void gettype()const=0;

   virtual void set_val(int val)=0;
   virtual int get_val()const=0;

};

class Base1: public base_interface
{
  public:
   Base1(int a):val1(a){
     cout<<"Constructing BASE1"<<endl;
   }
   virtual void gettype()const {
     cout<<"BASE1";
   }
  virtual void set_val(int val){
    val1=val;
  }
  virtual int get_val()const{
    return val1;
  }
  protected: int val1;

};

class Base2: public base_interface
{
  //same as Base1 with val2

};

class Derived: base_interface
{
   public: int val2;

   Derived(int b,base_interface* b):val2(b),dynbase(b)
   { //base not null - could use default base
     assert(b);
   }

   //set base
   base_interface* base(base_interface* b)
   { assert(b);
     base_interface* const tmp=dynbase;
     dynbase=b;
     return tmp;
   }

   virtual void gettype()const {
     cout<<"Derived inheriting from ";
     dynbase->gettype();
   }

   //delegating to base
   void set_val(int val)
   { assert(dynbase);
     dynbase->set_val(val);
   }
   int  get_val()const
   { assert(dynbase);
     return dynbase->get_val();
   }

};

void display(base_interface& d)
{
     cout<<"Type is"<<d->gettype()<<endl;
     //...

}

int main()
{
  Base1 b1(2);
  display(b1);

  Base2 b2(4);
  display(b2);

  Derived obj(1,&b1);
  display(obj);

  obj.base(&b2);
  display(obj);

}

I have not compiled the code but it should get you started

Thnx for your effort... it really helped... and googling about
Envelope/letter Idiom was helpful in swapping base classes..
 
Ad

Advertisements

L

Larry Evans

On 12/23/08 06:03, (e-mail address removed) wrote:
[snip]
Thnx for your effort... it really helped... and googling about
Envelope/letter Idiom was helpful in swapping base classes..

Maybe this would also be relevant:

http://article.gmane.org/gmane.comp.lib.boost.devel/89068

Section 12.7 of the reference contained the code snippet:

class B { int b; void f(); };
class C : *p { B* p; int c; };

and the meaning is that an instance of C behaves as if it
inherited from its *b member. IOW, C::f() would call C::b ->f().
IOW, C "delegates" calls to it's *p. This may be similar to
what Micahel DOUBEZ meant by "dispatching system".

Further relevant posts include:

http://thread.gmane.org/gmane.comp.lib.boost.devel/98583/focus=99347

An example of its use, implemented with a smart pointer
and manual delegating, is the dyn_inherit::inherit template use
shown here:

https://svn.boost.org/trac/boost/br...ns/boost/grammar_pipeline/eff/productions.hpp
 

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

Top