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的兄弟们,帮解惑一下。遇到这种情况应该怎么处理,只能从设计思路上绕开吗。