Link to home
Start Free TrialLog in
Avatar of mfenske17363
mfenske17363Flag for United States of America

asked on

A question of inheritance

I have run into a situation that I am trying to figure out a proper way to design/implement.

I start with a base class that has a method that relies on an override to to perform some task however the override does not neatly fit into a derived version of the base class.  The override fits better into an owner of an object made from the derived class.

Is it better to continue the chain of inheritance to a second derived class?

How about created another base/derived class pair and supplying it to the first pair so that a common method signature can be called (performing custom actions)?

Some other method to perform this?

Thanks
Avatar of AndyAinscow
AndyAinscow
Flag of Switzerland image

Is this your situation.
class A has a virtual member function Foo (no implementation?).
class B inherits from class A, but it makes no sense to code Foo in it.
class C has a member variable type is class B.  Class C is where an implementation of Foo makes sense.


If that is the case then why does class A require the virtual function Foo ?  Why not remove it from A and implement it in C ?
Avatar of mfenske17363

ASKER

In the above, class A has a method that will collect data and will pass that along to a custom validator as it were and will stop when the validator fails or a time out occurs.  That validator would be Foo and it makes more sense (in my context) to implement Foo above class B.
ASKER CERTIFIED SOLUTION
Avatar of mfenske17363
mfenske17363
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
So you moved away from inheritance in that case.  (That is using generics)
A clean inheritance implementation wasn't apparent to me.  Have any suggestions?
I couldn't see any justification for inheritance either (hence my initial remark).

Be pragmatic - you have a flexible solution with generics.  I'd suggest accepting your comment about using them.
Subsequent research showed that generics were the way to go.