Dynamic Inheritance Query..

Discussion in 'C++' started by praveenraj1987@gmail.com, Dec 23, 2008.

  1. Guest

    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..
     
    , Dec 23, 2008
    #1
    1. Advertising

  2. alfps Guest

    On 23 Des, 01:17, 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
     
    alfps, Dec 23, 2008
    #2
    1. Advertising

  3. Guest

    On Dec 23, 9:20 am, alfps <> wrote:
    > On 23 Des, 01:17, 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...
     
    , Dec 23, 2008
    #3
  4. Guest

    On Dec 23, 1:32 pm, Paavo Helde <> wrote:
    > kirjutas:
    >
    >
    >
    > > On Dec 23, 9:20 am, alfps <> wrote:
    > >> On 23 Des, 01:17, 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..
     
    , Dec 23, 2008
    #4
  5. a écrit :
    > On Dec 23, 1:32 pm, Paavo Helde <> wrote:
    >>> On Dec 23, 9:20 am, alfps <> wrote:
    >>>> On 23 Des, 01:17, 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
     
    Michael DOUBEZ, Dec 23, 2008
    #5
  6. Guest

    On Dec 23, 3:49 pm, Michael DOUBEZ <> wrote:
    > a écrit :
    >
    >
    >
    > > On Dec 23, 1:32 pm, Paavo Helde <> wrote:
    > >>> On Dec 23, 9:20 am, alfps <> wrote:
    > >>>> On 23 Des, 01:17, 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..
     
    , Dec 23, 2008
    #6
  7. Larry Evans Guest

    On 12/23/08 06:03, 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
     
    Larry Evans, Dec 25, 2008
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. maxw_cc
    Replies:
    1
    Views:
    3,160
    Martijn van Steenbergen
    Dec 21, 2003
  2. Anonymous
    Replies:
    0
    Views:
    1,516
    Anonymous
    Oct 13, 2005
  3. cppsks
    Replies:
    0
    Views:
    833
    cppsks
    Oct 27, 2004
  4. karthikbalaguru
    Replies:
    9
    Views:
    1,055
  5. Daniel Pitts
    Replies:
    27
    Views:
    1,927
    Mike Schilling
    Feb 27, 2008
Loading...

Share This Page