virtual methods and derived classes

K

Klaus

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
 
C

Christian Hackl

Klaus said:
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
 
N

Nick Keighley

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();
}
};
 
N

Nick Keighley

I assume you meant "two"!








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;
}
 
K

Klaus

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
 
N

Nick Keighley

could you leave some context in?

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! :)
 
J

James Kanze

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.
 

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,756
Messages
2,569,534
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top