[英]Purpose of implementing vtable in C instead of using C++
I've just started to dive into GStreamer framework and I discovered with quite surprise that despite the fact it written in pure C it uses objects and polymorphism.我刚刚开始深入研究 GStreamer 框架,我非常惊讶地发现,尽管它是用纯 C 编写的,但它使用了对象和多态性。 Then I discovered that it's possible to emulate some of C++ features like inheritance in C.
然后我发现可以在 C 中模拟一些 C++ 功能,例如 inheritance。 It's about implementing a virtual functions table by ourselves.
这是关于我们自己实现一个虚函数表。 I'm a little bit confused about this idea why would someone need it.
我对这个想法有点困惑,为什么有人需要它。 If I had to choose between implementing virtual tables in C and use C++ for the project I would definitely use tested and well known implementation of C++ language features.
如果我必须在 C 中实现虚拟表和在项目中使用 C++ 之间做出选择,我肯定会使用经过测试且众所周知的 C++ 语言特性的实现。 The whole idea seems to me like reinventing the wheel.
在我看来,整个想法就像重新发明轮子。 What are advantages of having macros like in C:
在 C 中使用宏有什么好处:
GST_IS_EVENT(obj)
GST_EVENT_CAST(obj)
over in C++:在 C++ 中:
dynamic_cast<Derived*>(&baseObj);
There are two things that I can think of:我能想到的有两点:
Are there any technical advantages of C with implementing vtable over C++? C 与 C++ 相比是否有任何技术优势?
The most important reason is, that C library ABI is also the standard library ABI at least on Linux and Windows and Mac.最重要的原因是,至少在 Linux 和 Windows 和 Mac 上,C 库 ABI 也是标准库 ABI。 In other words, these libraries can be used from any language (which supports using external libraries).
换句话说,可以从任何语言(支持使用外部库)中使用这些库。
C++ is different story. C++ 是另一回事。 For example, there is no standard ABI for C++ vtable.
例如,C++ vtable 没有标准 ABI。 Also, to support methods and function overloading and name spaces, C++ does name mangling for symbols.
此外,为了支持方法和 function 重载和名称空间,C++ 确实对符号进行了名称修改。 And then exceptions are yet another thing which affects how functions are called, and that may even need specific compiler switches even if using the same compiler.
然后异常是影响函数调用方式的另一件事,即使使用相同的编译器,它甚至可能需要特定的编译器开关。
It would be possible to just write "C" with C++, and export only extern "C"
symbols, but if you do that, you might as well use C to avoid complexity of mixing languages.可以只用 C++ 编写“C”,并只导出
extern "C"
符号,但如果这样做,您不妨使用 C 以避免混合语言的复杂性。
Probably because C++ offers many more features than they need for their project.可能是因为 C++ 提供的功能比他们项目所需的要多得多。
Maybe their OOP model is not the same as what C++ provides.也许他们的 OOP model 与 C++ 提供的不一样。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.