0

There are two classes in a library I use, lets call them class A and class B to keep things generic. Class B inherits from class A, so something like:

class A{
    ...
}

class B: public A{
    ...
}

So nothing fancy, but I can't edit either of these classes directly. Now I wanted to add some functionality to class A, so I subclass it as, say, myClassA, and add the functionality in the subclass. This works great when I use myClassA, but of course anywhere I use classB still inherits from the original class A, and not myClassA.

How can I make a subclass of class B that inherits from myClassA instead of the original classA? Would it work to simply have the subclass inherit from BOTH class B and myClassA, even though class B already inherits from the original class A?

EDIT: I just tried the "Inherit from BOTH" option and can confirm it does not work, at least not without a lot more work. Probably the diamond issue mentioned in the comments. So unless there is a way to override class B's inheritance of class A with myClassA, I may have to simply re-implement the changes I made to my subclass of class A in a subclass of class B, although that would be a violation of the DRY principle...

EDIT 2: To make this a little more concrete, take for example the standard "shape" class example. So in this case you'll have a base class of, say, rectangle, and rectangle is in tern inherited by a class of square. Now I want to add functionality, say a "move" function that shifts the position of the object by a specified amount. This would be the same for both rectangle and square, so ideally I'd implement it in the rectangle class, and square would inherit it. However, since I don't have access to the rectangle or square classes directly, I can only work with subclasses. Thus my desire to make a square subclass that inherits from my rectangle subclass rather than the base rectangle class.

EDIT 3: To restate and clarify the question in terms of the more concrete example, "Can I/How can I create a subclass of square but have it inherit from my_rectange (a rectangle subclass) INSTEAD OF inheriting from rectangle?" Or, to put it another way, "Can I/How can I replace a base class with something of my own if I can't modify the class directly?"

ibrewster
  • 3,482
  • 5
  • 42
  • 54
  • It sounds like you may be brushing up against [the diamond problem](https://en.wikipedia.org/wiki/Multiple_inheritance#The_diamond_problem) – scohe001 Aug 21 '18 at 19:04
  • Possible duplicate of [How does virtual inheritance solve the "diamond" (multiple inheritance) ambiguity?](https://stackoverflow.com/questions/2659116/how-does-virtual-inheritance-solve-the-diamond-multiple-inheritance-ambiguit) – scohe001 Aug 21 '18 at 19:06
  • @scohe001 possible. I'll have to look into it more to see if that will be an issue. I guess worst case I simply subclass `class B` as well, and re-implement the stuff I added in the subclass of class A. The question though was if there was a way to "override" the inheritance - say replace *your* class A ancestor with *my* class A, which would avoid any such issue. – ibrewster Aug 21 '18 at 19:09
  • Umm... could you just extend Class B instead of Class A? – 3Dave Aug 21 '18 at 19:15
  • 5
    XY problem? Why do you want to add functionality to the classes you are not supposed to (I imagine, they are in a library)? – SergeyA Aug 21 '18 at 19:16
  • @3Dave A reasonable suggestion, except there are places where I use class A (or my subclass thereof) directly. Class B changes the behavior of Class A in ways that are sometimes, but not always, desirable. – ibrewster Aug 21 '18 at 19:18
  • @SergeyA Because the base classes (which, yes, are in a library) don't work *quite* the way I want, specifically in this case in regards to file locks. – ibrewster Aug 21 '18 at 19:20
  • Might be obvious, but in your override you can specify which version of an overridden method you want to call. `classA::foo()`, etc. – 3Dave Aug 21 '18 at 19:28
  • You can't modify `A` nor `B`. Then use a new class `C` that DOESN'T inherits from A,B, but contains as a member an instance of `B`. The required additions may work with this object (use its methods). – Ripi2 Aug 21 '18 at 19:34
  • 2
    Sounds like you need composition instead of inheritance. – Hatted Rooster Aug 21 '18 at 19:36
  • @Ripi2 It's looking like your suggestion might be the best option here. Thanks :) – ibrewster Aug 21 '18 at 20:11

1 Answers1

0
template<class T /* T must be derived from A */> class myclsssA:T { /* ... */ }

new myclassA<B>()

This works sometimes depending on what you are doing. There's an extreme exactness to what this can do or not do, and there just aren't enough details in the question to even get close to knowing.

This makes myclassA the most-derived form so it can override all virtual methods successfully. But if you don't make the instances of B, it won't help at all.

Joshua
  • 40,822
  • 8
  • 72
  • 132