[英]C++: override class inheritance?
There are two classes in a library I use, lets call them class A
and class B
to keep things generic. 我使用的库中有两个类,可以将它们称为
class A
class B
和class B
以保持通用性。 Class B
inherits from class A
, so something like: Class B
继承自class A
,因此类似:
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. 现在,我想向类A添加一些功能,因此我将其子类化为
myClassA
,然后将该功能添加到子类中。 This works great when I use myClassA
, but of course anywhere I use classB
still inherits from the original class A
, and not myClassA
. 当我使用
myClassA
,这很好用,但是当然,我使用classB
任何地方仍然继承自原始class A
,而不是myClassA
。
How can I make a subclass of class B
that inherits from myClassA
instead of the original classA
? 如何制作从
myClassA
继承而不是原始classA
class B
类的子class B
? 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
? 即使
class B
已经从原始class A
继承,仅让子类从class B
和myClassA
继承都可以吗?
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... 因此,除非有一种方法可以用
myClassA
覆盖 class B
对class A
的继承, myClassA
我可能必须简单地重新实现对class B
的子类中对class A
的子class B
所做的更改,尽管这样做会违反DRY原理的...
EDIT 2: To make this a little more concrete, take for example the standard "shape" class example. 编辑2:为使这一点更加具体,例如以标准的“形状”类示例为例。 So in this case you'll have a base class of, say,
rectangle
, and rectangle
is in tern inherited by a class of square
. 因此,在这种情况下,您将有一个例如
rectangle
的基类,并且rectangle
是由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. rectangle
和square
都相同,因此理想情况下,我将在rectangle
类中实现它,而square
将继承它。 However, since I don't have access to the rectangle
or square
classes directly, I can only work with subclasses. 但是,由于我无法直接访问
rectangle
或square
类,因此只能使用子类。 Thus my desire to make a square
subclass that inherits from my rectangle
subclass rather than the base rectangle
class. 因此,我的愿望,使一个
square
子类, 我继承了rectangle
的子类,而不是基本rectangle
类。
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
?" 编辑3:用更具体的例子来重申和阐明问题,“我/我如何创建
square
的子类,但可以从my_rectange
( rectangle
子类)继承INSTEAD OF继承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?" 或者换句话说,“如果我不能直接修改基类,我可以/如何用自己的东西替换基类?”
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. 这使myclassA成为派生最多的形式,因此它可以成功覆盖所有虚拟方法。 But if you don't make the instances of B, it won't help at all.
但是,如果您不创建B的实例,那么它将完全无济于事。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.