有没有办法在运行时确定对象是否可以执行 C++ 中的方法?

发布于 2024-08-04 05:51:37 字数 425 浏览 4 评论 0原文

在 Perl 中,有一个 UNIVERSAL::can 方法,您可以调用任何类或对象来确定它是否能够执行某些操作:

sub FooBar::foo {}
print "Yup!\n" if FooBar->can('foo'); #prints "Yup!"

假设我在 C++ 中有一个基类指针,它可以是许多不同派生类中的任何一个,是有没有一种简单的方法可以完成类似的事情?我不想触及其他派生类中的任何内容,我只能更改调用该函数的基类中的区域以及支持该函数的一个派生类。

编辑:等等,这现在很明显了(别介意这个问题),我可以在返回表示未实现的数字的基数中实现它,然后在调用它时检查返回是否不是这个。我不知道为什么我会以如此复杂的方式思考事情。

我还想我可以从另一个实现了 foo 的类派生我的类,然后看看对该类的动态转换是否有效。

In Perl, there is a UNIVERSAL::can method you can call on any class or object to determine if it's able to do something:

sub FooBar::foo {}
print "Yup!\n" if FooBar->can('foo'); #prints "Yup!"

Say I have a base class pointer in C++ that can be any of a number of different derived classes, is there an easy way to accomplish something similar to this? I don't want to have to touch anything in the other derived classes, I can only change the area in the base class that calls the function, and the one derived class that supports it.

EDIT: Wait, this is obvious now (nevermind the question), I could just implement it in the base that returns a number representing UNIMPLEMENTED, then check that the return is not this when you call it. I'm not sure why I was thinking of things in such a complicated manner.

I was also thinking I would derive my class from another one that implemented foo then see if a dynamic cast to this class worked or not.

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

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

发布评论

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

评论(4

往事随风而去 2024-08-11 05:51:37

如果您有指向基类的指针或引用,则可以使用dynamic_cast 来查看它是哪个派生类(以及它支持哪个派生类的方法)。

If you have a pointer or reference to a base class, you can use dynamic_cast to see which derived class it is (and therefore which derived class's methods it supports).

蓝海 2024-08-11 05:51:37

如果可以向基类添加方法,则可以添加一个虚拟 bool can_foo() {return false;} 并在具有 foo 的子类中重写它以返回 true。

If you can add methods to the base class, you can add a virtual bool can_foo() {return false;} and override it in the subclass that has foo to return true.

友谊不毕业 2024-08-11 05:51:37

C++ 没有内置的运行时反射。您可以完全自由地将自己的反射实现构建到类层次结构中。这通常涉及一个静态映射,其中填充了名称和函数的列表。您必须手动注册您想要使用的每个函数,并在调用约定和函数签名方面保持一致。

C++ does not have built in run-time reflection. You are perfectly free to build your own reflection implementation into your class hierarchy. This usually involves a static map that gets populated with a list of names and functions. You have to manually register each function you want available, and have consistency as to the calling convention and function signature.

死开点丶别碍眼 2024-08-11 05:51:37

我相信最正确的方法是使用 typeid<>运算符并获取对 type_info 对象的引用,然后您可以将该(==运算符)与您希望关心的数据类型的所需 type_info 进行比较。

这不会为您提供方法级检查,并且确实需要您在启用 RTTI 的情况下进行构建(我相信在没有 RTTI 的情况下构建的对象上使用 typeid<> 会导致“未定义”行为),但是您可以是。

MSDN 有一个在线参考可以帮助您入门:http: //msdn.microsoft.com/en-us/library/b2ay8610%28VS.80%29.aspx

I believe the most-correct way would be to use the typeid<> operator and get a reference to the type_info object, and then you could compare that (== operator) to the desired type_info for the data types you wish to care about.

This doesn't give you method-level inspection, and does require that you've built with RTTI enabled (I believe that using typeid<> on an object that was built without RTTI results with "undefined" behavior), but there you are.

MSDN has an online reference to get you started : http://msdn.microsoft.com/en-us/library/b2ay8610%28VS.80%29.aspx

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