简体   繁体   English

如何在没有多态效应的情况下调用虚方法?

[英]How to call virtual method without polymorphic effect?

I have reference ref : 我有参考ref

Foo &ref = ..

I would like to call a method ref.say() which is defined as virtual in Foo and I know for sure it is overridden in child classes (because I wrote them as well). 我想调用一个方法ref.say() ,它在Foo定义为虚拟,我知道它确实在子类中被覆盖(因为我也写了它们)。

However I would like to call say as if it was not polymorphic, so the version which is defined in Foo, not in child classes. 不过,我想打电话say ,如果它是不是多态的,因此,这是Foo中定义的,而不是在子类的版本。

How to do it? 怎么做?

One thing that comes to my mind is to take pointer of ref , than dereference it and this trick should kill polymorphism, but I am not sure if this is guaranteed to take desired effect. 我想到的一件事是接受ref指针,而不是取消引用它,这个技巧应该杀死多态,但我不确定这是否能保证取得预期的效果。

Please note, I am not sitting inside Foo or any of its child, the Foo tree is external structure from my current POV. 请注意,我不是坐在Foo或其任何一个孩子里面,Foo树是我当前POV的外部结构。

How about 怎么样

ref.Foo::say();

This what you looking for? 这就是你要找的?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM