C ++:将方法虚拟化的含义 [英] C++ : implications of making a method virtual

查看:92
本文介绍了C ++:将方法虚拟化的含义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

应该是一个新手问题...

Should be a newbie question...

我想扩展现有类A中的现有代码,以覆盖现有方法A :: f().

I have existing code in an existing class, A, that I want to extend in order to override an existing method, A::f().

所以现在我想创建类B来覆盖f(),因为我不想只更改A :: f(),因为其他代码依赖于它.

So now I want to create class B to override f(), since I don't want to just change A::f() because other code depends on it.

为此,我相信我需要将A :: f()更改为虚拟方法.

To do this, I need to change A::f() to a virtual method, I believe.

我的问题是,除了允许动态调用方法(使用B的实现而不是A的实现)之外,将方法虚拟化还有其他含义吗?我在打破某种良好的编程习惯吗?这会影响其他任何尝试使用A :: f()的代码吗?

My question is besides allowing a method to be dynamically invoked (to use B's implementation and not A's) are there any other implications to making a method virtual? Am I breaking some kind of good programming practice? Will this affect any other code trying to use A::f()?

请让我知道.

谢谢, 杰布

edit:我的问题更多是将别人的方法虚拟化有什么不对吗?即使您没有更改其他人的实现,也仍然必须进入别人的现有代码并更改声明.

edit: my question was more along the lines of is there anything wrong with making someone else's method virtual? even though you're not changing someone else's implementation, you're still having to go into someone's existing code and make changes to the declaration.

推荐答案

如果将函数虚拟化为基类内部,则从其派生的任何内容也将对其进行虚拟化.

If you make the function virtual inside of the base class, anything that derives from it will also have it virtual.

虚拟后,如果创建A的实例,则它仍将调用A::f.

Once virtual, if you create an instance of A, then it will still call A::f.

如果创建B的实例并将其存储在类型为A*的指针中.然后您呼叫A*::->f,然后它将呼叫BB::f.

If you create an instance of B and store it in a pointer of type A*. And then you call A*::->f, then it will call B's B::f.

关于副作用,除了轻微(不明显)的性能损失外,可能不会有任何副作用.

As for side effects, there probably won't be any side effects, other than a slight (unnoticeable) performance loss.

也有非常小的副作用,可能有一个类C也从A派生而来,它可以实现C::f,并期望如果调用A*::->f,则它期望A::f可以叫做.但这不是很常见.

There is a very small side effect as well, there could be a class C that also derives from A, and it may implement C::f, and expect that if A*::->f was called, then it expects A::f to be called. But this is not very common.

但是很有可能,如果C存在,那么它根本不会实现C::f,在这种情况下,一切都很好.

But more than likely, if C exists, then it does not implement C::f at all, and in which case everything is fine.

但是请注意,如果您使用的是已编译的库,并且正在修改其头文件,则预期的工作可能不会成功.您将需要重新编译头文件和源文件.

Be careful though, if you are using an already compiled library and you are modifying it's header files, what you are expecting to work probably will not. You will need to recompile the header and source files.

您可以考虑执行以下操作以避免副作用:

You could consider doing the following to avoid side effects:

  1. 创建从A派生的类型A2并将其变为f虚拟
    • 使用类型为A2的指针代替A
    • 从类型A2派生B.
    • 通过这种方式,任何使用A的东西都将以保证的相同方式工作
  1. Create a type A2 that derives from A and make it's f virtual
    • Use pointers of type A2 instead of A
    • Derive B from type A2.
    • In this way anything that used A will work in the same way guaranteed

根据您的需要,您也许还可以使用has-a关系而不是is-a.

Depending on what you need you may also be able to use a has-a relationship instead of a is-a.

这篇关于C ++:将方法虚拟化的含义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆