如何检查 C++抽象方法是在运行时定义的

发布于 2024-10-05 09:06:15 字数 339 浏览 6 评论 0原文

如何检查 C++ 抽象方法是否在运行时定义

class ABase{
public:
 virtual void do1() = 0;
};

class BBase: public ABase{
public:
 virtual void do1(){}
};

class CBase: public ABase{
public:
};

ABase * base = rand() % 2 ? new BBase() : new CBase();
if(&(base->do1) != 0)
  base->do1();

这会产生错误。

谢谢, 最大限度

How to check if C++ abstract method is defined at runtime

class ABase{
public:
 virtual void do1() = 0;
};

class BBase: public ABase{
public:
 virtual void do1(){}
};

class CBase: public ABase{
public:
};

ABase * base = rand() % 2 ? new BBase() : new CBase();
if(&(base->do1) != 0)
  base->do1();

This gives error.

Thanks,
Max

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

踏雪无痕 2024-10-12 09:06:16

由于您无法实例化抽象类,因此您在运行时遇到的任何类都不会具有任何纯虚方法(除非您当时处于构造函数或析构函数中),它们都将被非纯虚方法覆盖覆盖者。没有什么可检查的。

As you can't instantiate an abstract class, any class you encounter at runtime will not have any pure virtual methods (unless you're in a constructor or destructor at the time), they'll all have been overriden with a non-pure overrider. There is nothing to check.

仅此而已 2024-10-12 09:06:16

必须实现抽象方法才能实例化该类。没有检查方法是否被实现这样的事情,编译器会为你做这件事。在这种情况下,您不能拥有 CBase 对象,因为它具有抽象方法。

An abstract method must be implemented in order for the class to be instantiated. There's no such thing as checking whether a method is implemented, the compiler will do this for you. In this case, you can't have a CBase object because it has abstract methods.

余生再见 2024-10-12 09:06:16

编译器不会让您创建未定义所有抽象方法的类型的实例。在上面的示例中,对 new CBase() 的调用将生成类似于“无法实例化抽象类型”的编译时错误。

The compiler won't let you create an instance of a type that doesn't define all abstract methods. In your example above the call to new CBase() will generate a compile time error along the lines of "cannot instantiate abstract type".

冷︶言冷语的世界 2024-10-12 09:06:16

CBase 是抽象的,因为它不重写 ABase::do1()。因此,您无法实例化它。或者更确切地说,如果您将 do1() 声明为虚拟,就会发生这种情况。但目前,它还无法编译。

不过,很高兴知道您为什么要这样做。

CBase is abstract because it does not override ABase::do1(). Therefore, you cannot instantiate it. Or rather, that's what would happen if you had declared do1() as virtual. But for now, it just won't compile.

Would be nice to know why you want to do this, though.

别念他 2024-10-12 09:06:16

您不需要检查该方法是否在运行时实现(无论如何在这种情况下都不能),因为 CBase 必须实现 do1() 才能满足从 ABase 的继承。

You don't need to check if the method is implemented at runtime (you can't in this case anyway) because CBase must implement do1() to satisfy inheriting from ABase.

嘿哥们儿 2024-10-12 09:06:16

您无法实例化CBase,因为它是...抽象的,即不实现其父级的所有纯虚函数。

最简单的可能是在 ABase 中定义一个存根实现:

class ABase {
public:
 void do1() { /* do nothing */ }
};

class BBase: public ABase {
public:
 void do1() { /* do something */ }
};

class CBase: public ABase {};

ABase * base = rand() % 2 ? new BBase() : new CBase();
base->do1();

You cannot instantiate the CBase since it's ... abstract, i.e. does not implement all pure virtual functions of its parents.

The easiest would probably be to define a stub implementation in the ABase:

class ABase {
public:
 void do1() { /* do nothing */ }
};

class BBase: public ABase {
public:
 void do1() { /* do something */ }
};

class CBase: public ABase {};

ABase * base = rand() % 2 ? new BBase() : new CBase();
base->do1();
凉风有信 2024-10-12 09:06:16

假设您记得将 do1() 设为虚拟,您可以在运行时检查 &ABase::do1、&BBase::do1 和 &CBase::do1。我不相信比较 &ABase::do1 和 &CBase::do1 会仅仅因为 CBase 没有覆盖该函数而返回相同的值,也不相信仅仅因为它在一个系统上覆盖该函数就意味着它总是会返回相同的值。

虽然您可能能够在运行时测试这些信息,但如果 CBase 类不是抽象的,您将无法使用这些信息来创建 CBase 类的对象,因为如果是抽象的,它将无法编译。

不过,您也可以用“C”方式来实现:拥有一个可以为 NULL 的函数指针表,检查其中一个是否为 NULL,然后创建此类实例的结构,或者如果不是则调用它。

Assuming you remember to make do1() virtual you could check &ABase::do1, &BBase::do1 and &CBase::do1 at runtime. I am not convinced that comparing &ABase::do1 and &CBase::do1 will return the same value just because CBase has not overridden the function, and whether just because it does on one system means it always will.

Whilst you may be able to test these at runtime you would not be able to use the information to create an object of the class CBase if it is not abstract as it will fail to compile when it is.

You could instead do it the "C" way though: have a table of function pointers that can be NULL, check if one of them is NULL, and create a struct of such an instance or invoke it if it isn't.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文