virtual methods and derived classes

Discussion in 'C++' started by Klaus, Jun 12, 2008.

  1. Klaus

    Klaus Guest

    Hi all:

    I want to have something like the following (incorrect!):

    class Base {
    public:
    virtual void MakeSomething();
    };

    class BehaviourModelOne {
    public:
    void MakeSomething();
    };


    class BehaviourModelOne {
    public:
    void MakeSomething();
    };


    class AnyDerived: public Base , BehaviourModelOne {
    };

    The idea is, that I want to create subclasses which use some special
    Funktions from ModelOne or ModelTwo and they could be used as normal
    virtual functions.

    But this is not allowed this way. Any idea how I can do this?

    Additional problem is, that Base comes from an library which should
    not changed!

    Thanks
    Klaus
     
    Klaus, Jun 12, 2008
    #1
    1. Advertising

  2. Klaus wrote:

    > I want to have something like the following (incorrect!):
    >
    > class Base {
    > public:
    > virtual void MakeSomething();
    > };
    >
    > class BehaviourModelOne {
    > public:
    > void MakeSomething();
    > };
    >
    >
    > class BehaviourModelOne {
    > public:
    > void MakeSomething();
    > };
    >
    >
    > class AnyDerived: public Base , BehaviourModelOne {
    > };
    >
    > The idea is, that I want to create subclasses which use some special
    > Funktions from ModelOne or ModelTwo and they could be used as normal
    > virtual functions.
    >
    > But this is not allowed this way. Any idea how I can do this?


    Are you looking for the Adapter design pattern? Just implement
    AnyDerived in terms of BehaviourModelOne while still deriving from Base:


    class AnyDerived : public Base
    {
    public:
    virtual void MakeSomething();
    private:
    BehaviourModelOne model;
    };

    void AnyDerived::MakeSomething()
    {
    model.MakeSomething();
    }

    In your example, you inherit privately from BehaviourModelOne. This is
    another way of implementing AnyDerived in terms of BehaviourModelOne.

    http://www.parashift.com/c -faq-lite/private-inheritance.html#faq-24.2


    --
    Christian Hackl
     
    Christian Hackl, Jun 12, 2008
    #2
    1. Advertising

  3. On 12 Jun, 10:45, Klaus <> wrote:
    > Hi all:
    >
    > I want to have something like the following (incorrect!):
    >
    > class Base {
    >    public:
    >      virtual void MakeSomething();
    >
    > };
    >
    > class BehaviourModelOne {
    >    public:
    >       void MakeSomething();
    >
    > };
    >
    > class BehaviourModelTwo {


    I assume you meant "two"!

    >    public:
    >       void MakeSomething();
    >
    > };
    >
    > class AnyDerived: public Base , BehaviourModelOne {
    >
    > };
    >
    > The idea is, that I want to create subclasses which use some special
    > Funktions from ModelOne or ModelTwo and they could be used as normal
    > virtual functions.
    >
    > But this is not allowed this way. Any idea how I can do this?
    >
    > Additional problem is, that Base comes from an library which should
    > not changed!


    is this any use:

    class Base {
    public:
    virtual void MakeSomething();

    };

    class BehaviourModelOne {
    private:
    void ModelMake();

    };


    class AnyDerived: public Base, public BehaviourModelOne {
    public:
    void MakeSomething()
    {
    ModelMake();
    }
    };


    --
    Nick Keighley
     
    Nick Keighley, Jun 12, 2008
    #3
  4. On 12 Jun, 12:41, Nick Keighley <>
    wrote:
    > On 12 Jun, 10:45, Klaus <> wrote:
    >
    >
    >
    >
    >
    > > Hi all:

    >
    > > I want to have something like the following (incorrect!):

    >
    > > class Base {
    > >    public:
    > >      virtual void MakeSomething();

    >
    > > };

    >
    > > class BehaviourModelOne {
    > >    public:
    > >       void MakeSomething();

    >
    > > };

    >
    > > class BehaviourModelTwo {

    >
    > I assume you meant "two"!
    >
    >
    >
    >
    >
    > >    public:
    > >       void MakeSomething();

    >
    > > };

    >
    > > class AnyDerived: public Base , BehaviourModelOne {

    >
    > > };

    >
    > > The idea is, that I want to create subclasses which use some special
    > > Funktions from ModelOne or ModelTwo and they could be used as normal
    > > virtual functions.

    >
    > > But this is not allowed this way. Any idea how I can do this?

    >
    > > Additional problem is, that Base comes from an library which should
    > > not changed!

    >
    > is this any use:
    >
    > class Base {
    >    public:
    >      virtual void MakeSomething();
    >
    > };
    >
    > class BehaviourModelOne {
    >    private:
    >       void ModelMake();
    >
    > };
    >
    > class AnyDerived: public Base, public BehaviourModelOne {
    >    public:
    >        void MakeSomething()
    >        {
    >             ModelMake();
    >        }
    >
    > };


    or perhaps

    class AnyDerived: public Base
    {
    public:
    AnyDerived(BehaviourModel& the_model): model(the_model)
    {}

    MakeSomething()
    {
    model.MakeSomething();
    }

    private:
    BehaviourModel& model;
    }


    --
    Nick Keighley
     
    Nick Keighley, Jun 12, 2008
    #4
  5. Klaus

    Klaus Guest

    All examples you add need active overwriting some functions in
    AnyDerived. This is not what would have. If I have to modify all my
    derived classes by hand, not only by deriving from one of two
    BehaviourModels, so I simply could write direct the needed function
    call inside the AnyDerived class. Or have I something overseen?

    My wish is: Havinging virtual base mehthods, set the vtable to
    BehaviourModel by deriving from one of some of them. I hope that
    becomes a bit clearer :)

    Regards
    Klaus
     
    Klaus, Jun 12, 2008
    #5
  6. could you leave some context in?

    On 12 Jun, 19:17, Klaus <> wrote:

    > All examples you add need active overwriting some functions in
    > AnyDerived.


    I'm probably missing something, that to you is very obvious.
    What does "active overwriting" mean? Yes you have to write the
    derived classes to match one of the (three so far) techniques
    suggested. But you only do this once. Changeing the behaviour
    model classes will change the behaviour of all the derived classes
    with no change to the derived classes. You seem to change behaviour
    by deriving from different behaviour classes. This involves
    chaging all the derived classes!

    But again I'm not sure I understand what you want.


    > This is not what would have. If I have to modify all my
    > derived classes by hand, not only by deriving from one of two
    > BehaviourModels, so I simply could write direct the needed function
    > call inside the AnyDerived class. Or have I something overseen?


    I don't understand


    > My wish is: Havinging virtual base mehthods, set the vtable to
    > BehaviourModel by deriving from one of some of them. I hope that
    > becomes a bit clearer :)


    'fraid not! :)


    --
    Nick Keighley
     
    Nick Keighley, Jun 13, 2008
    #6
  7. Klaus

    James Kanze Guest

    On Jun 12, 8:17 pm, Klaus <> wrote:
    > All examples you add need active overwriting some functions in
    > AnyDerived. This is not what would have.


    In other words, you would prefer that the compiler just pick up
    any random function which happened to have the same name.

    > If I have to modify all my derived classes by hand, not only
    > by deriving from one of two BehaviourModels, so I simply could
    > write direct the needed function call inside the AnyDerived
    > class. Or have I something overseen?


    Templates? If this is all the derived classes do, then you can
    simply use a template for them:

    template< typename Impl >
    class Derived : public Base
    {
    public:
    virtual void doSomething() { Base::doSomething() ; }
    } ;

    If they have additional behavior, you can still use something
    like the above, but with the final class deriving from the
    template instance, rather than from Base directly.

    > My wish is: Havinging virtual base mehthods, set the vtable to
    > BehaviourModel by deriving from one of some of them. I hope that
    > becomes a bit clearer :)


    Not too much.

    --
    James Kanze (GABI Software) email:
    Conseils en informatique orient�e objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place S�mard, 78210 St.-Cyr-l'�cole, France, +33 (0)1 30 23 00 34
     
    James Kanze, Jun 13, 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.

Share This Page