C++11 结构体 变长数组 继承
示例代码如下:
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的兄弟们,帮解惑一下。遇到这种情况应该怎么处理,只能从设计思路上绕开吗。
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的兄弟们,帮解惑一下。遇到这种情况应该怎么处理,只能从设计思路上绕开吗。
...全文
相关推荐
Google-CPP 编码规范(中文版)最新版.pdf
#### 六、其他C++特性- **右值引用**:利用右值引用来支持移动语义,提高效率。- **变长数组和alloca()**:避免使用这些功能,因为它们可能导致堆栈溢出等安全问题。
Google C++编程规范
**3.5 结构体VS.类**- 结构体通常用于存储数据,类则包含数据和操作这些数据的方法。**3.6 继承**- 单继承优于多继承。- 谨慎使用公有继承。
深入理解C++11完整版
统一的初始化语法:C++11使得初始化语法统一,不仅可以用在对象构造,也可以用于数组和结构体等的初始化。12.
google c++ style guide PDF版(带目录及标签目录)
()- **规则**: 避免使用可变长度数组和`alloca()`,因为它们可能导致内存安全问题。
Google_C++_Style_Guide
右值引用和引用参数:C++11引入的新特性,如何正确使用。25. 函数重载、默认参数:函数重载和默认参数的使用。26. 变长数组和alloca():变长数组的使用以及动态内存分配。27.


