Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   Dynamic Inheritance Query.. (http://www.velocityreviews.com/forums/t649735-dynamic-inheritance-query.html)

praveenraj1987@gmail.com 12-23-2008 12:17 AM

Dynamic Inheritance Query..
 
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..

alfps 12-23-2008 04:20 AM

Re: Dynamic Inheritance Query..
 
On 23 Des, 01:17, praveenraj1...@gmail.com 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.
> 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

praveenraj1987@gmail.com 12-23-2008 07:54 AM

Re: Dynamic Inheritance Query..
 
On Dec 23, 9:20*am, alfps <alf.p.steinb...@gmail.com> wrote:
> On 23 Des, 01:17, praveenraj1...@gmail.com 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.
> > 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


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...

praveenraj1987@gmail.com 12-23-2008 10:04 AM

Re: Dynamic Inheritance Query..
 
On Dec 23, 1:32*pm, Paavo Helde <pa...@nospam.please.ee> wrote:
> praveenraj1...@gmail.com kirjutas:
>
>
>
> > On Dec 23, 9:20*am, alfps <alf.p.steinb...@gmail.com> wrote:
> >> On 23 Des, 01:17, praveenraj1...@gmail.com 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.
> >> > 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

>
> > 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..

>
> 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..

Michael DOUBEZ 12-23-2008 10:49 AM

Re: Dynamic Inheritance Query..
 
praveenraj1987@gmail.com a écrit :
> On Dec 23, 1:32 pm, Paavo Helde <pa...@nospam.please.ee> wrote:
>>> On Dec 23, 9:20 am, alfps <alf.p.steinb...@gmail.com> wrote:
>>>> On 23 Des, 01:17, praveenraj1...@gmail.com 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

praveenraj1987@gmail.com 12-23-2008 12:03 PM

Re: Dynamic Inheritance Query..
 
On Dec 23, 3:49*pm, Michael DOUBEZ <michael.dou...@free.fr> wrote:
> praveenraj1...@gmail.com a écrit :
>
>
>
> > On Dec 23, 1:32 pm, Paavo Helde <pa...@nospam.please.ee> wrote:
> >>> On Dec 23, 9:20 am, alfps <alf.p.steinb...@gmail.com> wrote:
> >>>> On 23 Des, 01:17, praveenraj1...@gmail.com 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..

Larry Evans 12-25-2008 02:52 AM

Re: Dynamic Inheritance Query..
 
On 12/23/08 06:03, praveenraj1987@gmail.com 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....st.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.l...83/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/bro...roductions.hpp


All times are GMT. The time now is 05:07 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.