c – 为什么这不是POD类型?

前端之家收集整理的这篇文章主要介绍了c – 为什么这不是POD类型?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我在g 4.6.2(mingw)上运行g -std = c 0x pod_test.cpp.我在A4上收到错误.为什么不是A4 POD?
#include <iostream>
#include <new>
#include <cstring>

using namespace std;

struct A {
    int a,b;
    char c;
};
struct A2 {
    short buf[1];
};
struct A3:A {
};
struct A4:A {
    short buf[1];
};
static_assert(std::is_pod<A>::value,"Struct must be a POD type");
static_assert(std::is_pod<A2>::value,"Struct must be a POD type");
static_assert(std::is_pod<A3>::value,"Struct must be a POD type");
static_assert(std::is_pod<A4>::value,"Struct must be a POD type");

int main(){}

解决方法

它不是POD,因为它违反了标准布局类的规则:

— either has no non-static data members in the most derived class and
at most one base class with non-static data members,or has no base
classes with non-static data members

继承格中只有一个类可以有非静态数据成员.在这种情况下,两者都是A和A4有.

原文链接:https://www.f2er.com/c/112882.html

猜你在找的C&C++相关文章