What’s associated between C and C++ -- POD types

来源:互联网 发布:黑道圣徒4mac百度云 编辑:程序博客网 时间:2024/05/16 05:23

  TheISO C++ Standard (ISO14882) has been on and around for almost 10 yearsby now, and the standard placed a huge gap between C and C++. AlthoughC++ was said to be acting, in some aspects and concepts, as closely asC, yet compatibility with C was none of the goals for C++ to achieve.POD is an acronym which is used in the standard but not officiallydefined (For a compiler, the standard won’t even compile, will it?). Itstands for Plain Old Data, which, the way I see it, refers to what’scalled a struct in C. And we will see how C++ deals with POD types inthe rest of the article.

  Whatis a POD type? First and foremost, we should use “struct” to specifythe identifier, not “class”. If we use “class” to specify theidentifier, we’ll inevitably use “public” to issue the members, whichwill make this type non-POD. Then, make sure that the struct must NOTcontain any other non-POD types as data members. It can neither inheritfrom nor be inherited by other classes or structs, no matter POD ornon-POD. And what’s more, virtual functions are not allowed in PODtypes. A brief conclusion, a POD looks just like a C struct.

  Whetheryou realized or not, C++ compiler treats a POD type no different thanthe way C compiler treats a struct. For example, the data memberswithin a struct are presented in the order of their declarations, as iswithin a POD; we can copy or initialize a struct by calling memcpy ormemmove, same as a POD by std::memcpy or std::memmove; the size of astruct, as well as that of a POD, equals to the summer of the size ofthe data members (For non-POD types, there are no such assumptions.).

  Aquestion may arise that does a C++ compiler treat a struct and a classdifferently? For a struct that is a POD, yes! As is stated above, aC-struct, when it’s compiled and executed, performs a strictly-in-orderlayout, unaltered by the compiler; yet in a C++ class, the members arereordered by the compiler (in a way the compiler sees fit). To put itclear, there are no such guarantee that members (including data membersand member functions) in a C++ class will be compiled in the order wedeclared it, and nor will public members be put before or after privatemembers. For classes with virtual or pure virtual functions,(explicitly declared or inherited from another class), the place ofvptr (a widely-used name for what points to a so-called virtualfunction table) is implementation-defined.

  So,what’s benefitted from using POD types? A POD type provides a chancefor C++ programs to communicate and interoperate with C APIs. Projectsthat are implemented in C don’t have to be changed to fit C++. And, ifwe take full advantages of the C linking directives (or to say, extern"C"), the existing C code won’t even need recompiling! For writers ofcompilers or implementations, only one object model is needed, sincethe semantics for a C struct and a POD type in C++ are the same.

  Thereare indeed similarities and differences between C and C++. They oftenprovide us with convenience, as well as confusion, doubt, and troubles.In the next article of this series, we will talk about type casts in Cand C++.