加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-16 06:03:06 所属栏目:百科 来源:网络整理
导读:我在g 4.6.2(mingw)上运行g -std = c 0x pod_test.cpp.我在A4上收到错误.为什么不是A4 POD? #include iostream#include new#include cstringusing namespace std;struct A { int a,b; char c;};struct A2 { short buf[1];};struct A3:A {};struct A4:A { sh
我在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有.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读