告警信息清楚的说明了上述代码存在的问题,并且指出问题的根源在于传入了non-POD类型的实参。那么POD类型是什么含义呢?C++标准对其定义如下:
Arithmetic types (3.9.1), enumeration types, pointer types, and pointer to member types (3.9.2), and cv-qualified versions of these types (3.9.3) are collectively called scalar types. Scalar types, POD-struct types, POD-union types (clause 9), arrays of such types and cv-qualified versions of these types (3.9.3) are collectively called POD types.
实在是太晦涩!幸运的是,在google上找到了对POD type比较通俗的说明:
[26.7] What is a "POD type"?
A type that consists of nothing but Plain Old Data.
A POD type is a C++ type that has an equivalent in C, and that uses the same rules as C uses for initialization, copying, layout, and addressing.
As an example, the C declaration struct Fred x; does not initialize the members of the Fred variable x. To make this same behavior happen in C++, Fred would need to not have any constructors. Similarly to make the C++ version of copying the same as the C version, the C++ Fred must not have overloaded the assignment operator. To make sure the other rules match, the C++ version must not have virtual functions, base classes, non-static members that are private or protected, or a destructor. It can, however, have static data members, static member functions, and non-static non-virtual member functions.
也就是说,一个POD type是指在C语言中有对应定义的类型,而non-POD类型的变量可能具有一些C语言没有的动作。由于编译器无法检查可变参数的类型,因此,它无法针对类型插入相应的动作,从而可能导致运行时期的问题。
6 考核点
字符指针 字符串
7 试题
下列正确的是 BD
A
char arraychar[2];
arraychar[0] = 0x10;
arraychar[1] = 0;
printf("%s", arraychar[0]);
B
char arraychar[2];
arraychar[0] = 0x10;
arraychar[1] = 0;
printf("%s", arraychar);
C
char arraychar[2];
arraychar[0] = 0x10;
arraychar[1] = 0;
string str = arraychar;
printf("%s", str);
D
char arraychar[2];
arraychar[0] = 0x10;
arraychar[1] = 0;
string str = arraychar;
printf("%s", str.c_str());
阅读(426) | 评论(0) | 转发(0) |