抽象类中的虚拟类
你好:)我想问,是否可以做这样的事情:
我有基类(父类)A和其他三个类(子类)BCD 在A类中,我有虚函数,没关系。 但如果我需要虚拟课程怎么办?
class A
{
public:
virtual int func1()=0;
virtual int func2()=0;
virtual class AB; // !!!!???
};
class B
{
public:
int func1();
int func2();
class AB
{
public:
....
};
};
BCD 类与 B 类相同。现在,我想创建类实例,它应该自动将类“重定向”到 B 或 CD 等的实例(如函数)。
是否可以 ?我希望你理解:)非常感谢你的回答。
Hello :) i would like to ask, if it's posible to make something like this:
i have base class (parent) A and three other classes (childs) B C D
in class A, i have virtual functions, that's ok.
but what if i need a virtual class ?
class A
{
public:
virtual int func1()=0;
virtual int func2()=0;
virtual class AB; // !!!!???
};
class B
{
public:
int func1();
int func2();
class AB
{
public:
....
};
};
classes B C D are same as class B. Now, i would like to create class instance and it should automaticly "redirect" class to instance of B or C D etc like functions.
is it possible ? i hope, you understand :) Thank you very much for answer.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这根本上是不可能的。虚拟函数的调用是在运行时确定的。类在编译时更改程序的行为。除非运行时和编译时是同一时间,即使用 JIT 或其他动态代码生成器,否则无法在运行时确定编译时。在标准 C++ 中,这是不可能的。
您可以做的是拥有一个基类 AB,并使用一个虚函数来创建一个保证从该基类继承的类,然后返回一个指向该基类的指针。
This is fundamentally impossible. A virtual function call is determined at runtime. A class changes the behaviour of the program at compile-time. You can't make a compile-time determination at runtime unless runtime and compiletime are the same time, i.e. using a JIT or other dynamic code generators. In standard C++, this is impossible.
What you CAN do is have a base class AB, with a virtual function that creates a class that is guaranteed to inherit from this base class, and then return a pointer to that.