C++11 结构体 变长数组 继承

七十二寨寨主 七十二寨寨主 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
示例代码如下:

x64 [ ~/test ]# cat main.cpp
#include <iostream>
using namespace std;

struct A
{
int nLen;
char buf[];
};

struct B: public A
{
int nValue;
};

int
main(int argc, char *argv[])
{
return 0;
}

使用g++ 4.1.2编译(C++99标准)通过
x64 [ ~/test ]# g++ -v
gcc version 4.1.2
x64 [ ~/test ]# g++ main.cpp
x64 [ ~/test ]#

使用g++ 8.2.0编译(标准是 C++11?? C++14??)
x64 [ ~/test ]# /opt/bin/g++ -v
gcc version 8.2.0 (GCC)
x64 [ ~/test ]# /opt/bin/g++ main.cpp
main.cpp:7:14: error: flexible array member 'A::buf' not at end of 'struct B'
char buf[];
^
main.cpp:12:9: note: next member 'int B::nValue' declared here
int nValue;
^~~~~~
main.cpp:10:8: note: in the definition of 'struct B'
struct B: public A
^
x64 [ ~/test ]#


有了解C++11或C++14的兄弟们,帮解惑一下。遇到这种情况应该怎么处理,只能从设计思路上绕开吗。
...全文
评论 18
收藏 已收藏
分享
返回顶部
七十二寨寨主
C++ 语言 65,214 成员 250,514 内容