如何实现 C++ C中的虚函数
C++ 语言提供虚拟
函数。在纯C语言实现的限制下,如何才能达到类似的效果呢?
The C++ language provides virtual
functions. Within the constraints of a pure C language implementation, how can a similar effect be achieved?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
盗自此处。
从 C++ 类
可以派生出 C 代码片段。
A 类
的三个 C++ 成员函数使用外联(独立)代码重写,并按地址收集到名为A_functable
的结构中。A
的数据成员与函数表组合成一个名为A
的 C 结构体。比必要的更详细,但它表达了要点。
Stolen from here.
From the C++ class
a C code fragment can be derived. The three C++ member functions of
class A
are rewritten using out-of-line (standalone) code and collected by address into a struct namedA_functable
. The data members ofA
and combined with the function table into a C struct namedA
.More elaborate than necessary, but it gets the point across.
@GCC ....虚函数在对象的基类中声明,然后在子类中“覆盖”或实现。即,假设您有车辆基类,并且创建了两个子类:摩托车和汽车。基类将声明一个虚拟函数 AddTires() 然后子类将实现该函数,并且每个子类将以不同的方式实现它。汽车有 4 个轮子,而摩托车有 2 个轮子。不过,我无法为您提供 C 或 C++ 的语法。希望这有帮助
@GCC....A virtual function is declared in the Base class of an object and is then "overriden" or implemented in the sub classes. i.e., say you have Vehicle Base class and you create two sub-classes, Motorcycle and, Automobile. The Base class would declare a virtual function of AddTires() Then the Sub Classes would implement this function and each sub class would implement it differently. A car has 4 wheels, where a motorcycle has 2. I can't give you the syntax for C or C++, though. Hope this helps
虚函数是C++面向对象的一个特性。它们指的是依赖于特定对象实例的方法,而不是您当前携带它们的类型。
换句话说:如果将一个对象实例化为 Bar,然后将其转换为 Foo,虚拟方法仍将是实例化时的方法(在 Bar 中定义),而其他方法将是来自 Foo 的方法。
虚拟函数通常通过 vtable 的方式实现(您需要对此进行更多研究;))。
您可以通过使用结构作为穷人的对象并在其中保存函数指针来在 C 中模拟类似的事情。
(更准确地说,非虚函数使得该方法应该从哪个类中获取变得不明确,但实际上我相信 C++ 使用当前类型。)
Virtual functions are a feature of C++'s object orientation. They refer to methods that depend on a specific object instance rather than what type you're currently carrying them around as.
In other words: if you instantiate an object as Bar, then cast it to Foo, virtual methods will still be the ones they were at instantiation (defined in Bar), while other methods will be the ones from Foo.
Virtual functions are typically implemented by way of vtables (that's for you to do more research on ;)).
You can simulate similar things in C by using structs as poor man's objects and saving function pointers in them.
(More correctly, non-virtual functions make it ambiguous which class the method should be taken from, but in practice I believe C++ uses the current type.)
这里是对虚拟函数的描述。
无法在普通 C 中实现虚函数,因为 C 没有继承的概念。
更新:
正如下面的评论中所讨论的,可以使用结构和函数指针来执行与直接 C 中的虚拟函数类似的操作。然而,如果您习惯了像 C++ 这样具有“真正的”虚函数的语言,您可能会发现 C 近似不太优雅且难以使用。
Here is a description of what virtual functions are.
There is no way to implement virtual functions in plain C, because C has no notion of inheritance.
Update:
As is discussed in the comments below, it is possible to do something similar to virtual functions in straight C using structures and function pointers. However, if you are accustomed to a language like C++ that has "true" virtual functions, you will probably find the C approximation far less elegant and harder to use.