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++.
- What’s associated between C and C++ -- POD types
- What’s associated between C and C++ -- POD types
- What's the difference between gcc and g++/gcc-c++?
- What's the difference between Interface and an abstract class in visual c#?
- In C++, what’s the difference between an inline function and a macro?
- What is the difference between XML and C or C ?
- What's the difference between “Normal Reload”, “Hard Reload”, and “Empty Cache and Hard Reload” in c
- 『C/C++』POD types
- 28What are mutable and immutable types in Objective C?
- What is the difference between fprintf and vfprintf in C++?
- what is the difference between definition and declaration in c
- What are the differences between struct and class in C++?
- What's the difference between declaring and defining something in C and C++?(C和C++中,声明和定义的区别)
- What's difference between PMP and PRINCE2
- What exactly is the difference between “pass by reference” in C and in C++?
- What is the difference between #import and #include in Objective-C?
- What are the differences between a pointer variable and a reference variable in C++?
- What are the differences between a pointer variable and a reference variable in C++?
- asp.net WebService的特点和运行原理
- java 时间函数
- Linux下优秀的IDE开发环境
- Sys.WebForms.PageRequestManagerParserErrorException
- Java核心API需要掌握的程度
- What’s associated between C and C++ -- POD types
- reactos操作系统实现(64)
- 字符串查找(相关)
- 人生就象卫生纸、没事尽量少扯
- 没有继承!
- 阿甘的巧克力盒子: 手机视频文件格式解析之 3GP/MP4
- CentOS 5.3上yum安装Apache+php+Mysql+phpMyAdmin
- MapXtreme 输出为图片时,注意装饰层
- 回调函数