tjw*_*992 10 c++ polymorphism inheritance templates
通过混合静态和动态多态(模板和继承),我遇到了一种奇怪的技术,其功能类似于C++中的常规静态多态,除了在创建新对象后子类的成员仍然可见.
请考虑以下示例:
Base.h:
#include <iostream>
class Base {
public:
virtual ~Base() {}
virtual void say_hello() {
std::cout << "Hello from Base!" << std::endl;
}
};
Run Code Online (Sandbox Code Playgroud)
Class1.h:
#include "Base.h"
#include <iostream>
class Class1 : public Base {
public:
virtual void say_hello() {
std::cout << "Hello from Class1!" << std::endl;
}
int x = 1;
};
Run Code Online (Sandbox Code Playgroud)
Class2.h:
#include "Base.h"
#include <iostream>
class Class2 : public Base {
public:
virtual void say_hello() {
std::cout << "Hello from Class2!" << std::endl;
}
int y = 2;
};
Run Code Online (Sandbox Code Playgroud)
事情变得有趣......
ClassX.h
template <class T>
class ClassX : public T {
public:
int z = 3;
};
Run Code Online (Sandbox Code Playgroud)
通过以这样的方式实现classX,它可以动态地从任何东西继承它允许发生一些奇怪的事情.请参阅下面的示例,显示它正在使用中.
main.cpp中
#include <iostream>
#include "Base.h"
#include "Class1.h"
#include "Class2.h"
#include "ClassX.h"
using namespace std;
int main(int argc, char* argv[]) {
Base* b = new Base;
b->say_hello();
// Regular polymorphism in action
Base* c1 = new Class1;
c1->say_hello(); // Aware that this is Class1
//cout << c1->x << endl; // Doesn't work! Not visible from here
Base* c2 = new Class2;
c2->say_hello(); // Aware that this is Class2
//cout << c2->y << endl; // Doesn't work! Not visible from here
// Hyper polymorphism!? Not sure what to call this.
ClassX<Class1> cx1;
cx1.say_hello(); // Aware that this is Class1
cout << cx1.x << endl; // The member variable is visible!
cout << cx1.z << endl; // Also available :)
ClassX<Class2> cx2;
cx2.say_hello(); // Aware that this is Class2
cout << cx2.y << endl; // The member variable is visible!
cout << cx2.z << endl; // Also available :)
// ALWAYS delete objects created with "new" or shame on yew.
delete b;
delete c1;
delete c2;
}
Run Code Online (Sandbox Code Playgroud)
我想知道为什么我以前从未见过这种技术?我从来没有见过有人尝试使用这样的模板继承未知类:
template <class T>
class Name : public T {
// Implementation
};
Run Code Online (Sandbox Code Playgroud)
这种技术有名称,它的用途是什么?
我只是试一试,因为我知道C++的规则,我没有看到它无法工作的原因.因为我似乎无法在任何地方找到它的名称,我将称之为"超多态性":)
编辑:抱歉误解了OP代码的重要部分.对我感到羞耻.删除了我的答案的错误部分.但是,以下仍然有......
Imho你的比较是不公平的
Base* c2 = new Class2;
//cout << c2->y << endl; // Doesn't work! Not visible from here
ClassX<Class1> cx1;
cout << cx1.x << endl; // The member variable is visible!
Run Code Online (Sandbox Code Playgroud)
这是两个完全不同的案例.公平的比较是
Base* c2 = new Class2;
//cout << c2->y << endl; // Doesn't work! Not visible from here
Run Code Online (Sandbox Code Playgroud)
VS
Base* cx1 = new ClassX<Class1>();
//cout << cx1->x << endl; // Wont work as well !!!
Run Code Online (Sandbox Code Playgroud)
(见这里的例子)或
Class2 c2;
cout << c2.y << endl; // Works of course
Run Code Online (Sandbox Code Playgroud)
VS
ClassX<Class1> cx1;
cout << cx1.x << endl; // Works of course as well !!!
Run Code Online (Sandbox Code Playgroud)
也就是说,这种技术可能有它的应用.例如,当您需要向许多不同的基类添加相同的功能时,您在注释中提到的情况.
这种技术有名称,它的用途是什么?
Afaik没有这个名字.正如其他人在评论中提到的那样,这些用途是用相同的功能来装饰许多不同的类.
我必须承认,只有在@Amadeus指出我的答案的一半是错误的之后,我才完全理解OPs代码中的方法.虽然这不是一个众所周知且常用的技术,但它是相当多的努力(两次继承加上一个模板)而不是太多的收益.虽然它在某些特殊情况下可能有用.