C++ 有许多features( abstract class, virtual deconstuctor,RTTI, virtual inheritance)完全用C 去模拟一个C++ 其实是很难。
下面列一个表,如何用C 去模拟C++的一些主要功能:
Feature in C++ | Implemented in C |
Class | structure |
data member | data member |
static_ data member | global member and link |
membor function | function point |
constructor function | global access entry |
destructor function | x |
access decorator | x |
Inheritance | Composit |
Ploymorphism | Overwite the function point |
RTTI |
OK |
下面一个例子来自于 codeproject:
http://www.codeproject.com/KB/cpp/InheritancePolymorphismC.aspx