在C ++中,追溯定义已定义类的超类的最接近的方法是什么? [英] What's the closest thing in C++ to retroactively defining a superclass of a defined class?
问题描述
假设我有课
class A {
protected:
int x,y;
double z,w;
public:
void foo();
void bar();
void baz();
};
在我的代码和其他代码中定义和使用.现在,我想编写一些可以很好地在A上运行的库,但实际上它更通用,并且可以在以下库上运行:
defined and used in my code and the code of others. Now, I want to write some library which could very well operate on A's, but it's actually more general, and would be able to operate on:
class B {
protected:
int y;
double z;
public:
void bar();
};
并且我确实希望我的库是通用的,所以我定义了一个B类,这就是它的API所采用的.
and I do want my library to be general, so I define a B class and that's what its APIs take.
我想告诉编译器-不是我不再控制的A定义,而是其他地方,可能是B的定义:
I would like to be able to tell the compiler - not in the definition of A which I no longer control, but elsewhere, probably in the definition of B:
看,请尝试将
B
视为A
的超类.因此,尤其是将其布置在内存中,这样,如果我将A *
重新解释为B *
,我的代码就会期望B *
会工作.然后请实际接受A *
作为B *
(和A&
作为B&
等).
Look, please try to think of
B
as a superclass ofA
. Thus, in particular, lay it out in memory so that if I reinterpret anA*
as aB*
, my code expectingB*
s would work. And please then actually acceptA*
as aB*
(andA&
as aB&
etc.).
在C ++中,我们可以通过另一种方式来完成此操作,即,如果B是我们无法控制的类,则可以对 class A执行子类为已知类"的操作:public B {...}
;而且我知道C ++没有相反的机制-将已知的类A替换为新的类B".我的问题是-这种机制最接近的近似值是什么?
In C++ we can do this the other way, i.e. if B is the class we don't control we can perform a "subclass a known class" operation with class A : public B { ... }
; and I know C++ doesn't have the opposite mechanism - "superclass a known class A by a new class B". My question is - what's the closest achievable approximation of this mechanism?
注释:
- 这完全是编译时,而不是运行时.
- 对
A类
的任何更改都不能没有.我只能修改B
的定义以及同时了解A
和B
的代码.其他人仍然会使用A
类,如果我希望我的代码与他们的代码交互,我也会这样做. - 最好应可伸缩"到多个超类.所以也许我也有
class C {protected:int x;双w;公开:无效的baz();}
,它的行为也应类似于A
的超类.
- This is all strictly compile-time, not run-time.
- There can be no changes whatsoever to
class A
. I can only modify the definition ofB
and code that knows about bothA
andB
. Other people will still use classA
, and so will I if I want my code to interact with theirs. - This should preferably be "scalable" to multiple superclasses. So maybe I also have
class C { protected: int x; double w; public: void baz(); }
which should also behave like a superclass ofA
.
推荐答案
您可以执行以下操作:
class C
{
struct Interface
{
virtual void bar() = 0;
virtual ~Interface(){}
};
template <class T>
struct Interfacer : Interface
{
T t;
Interfacer(T t):t(t){}
void bar() { t.bar(); }
};
std::unique_ptr<Interface> interface;
public:
template <class T>
C(const T & t): interface(new Interfacer<T>(t)){}
void bar() { interface->bar(); }
};
这个想法是在表皮下使用类型擦除(即 Interface
和 Interfacer< T>
类),以允许 C
采取任何您可以调用 bar
的方式,然后您的库将接受 C
类型的对象.
The idea is to use type-erasure (that's the Interface
and Interfacer<T>
classes) under the covers to allow C
to take anything that you can call bar
on and then your library will take objects of type C
.
这篇关于在C ++中,追溯定义已定义类的超类的最接近的方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!