我有一个带有一些参数的构造函数.我曾假设它们是按照列出的顺序构建的,但在一种情况下,它们似乎是反向构建的,导致中止.当我反转参数时,程序停止中止.这是我正在使用的语法示例.问题是,在这种情况下, a_ 需要在 b_ 之前初始化.你能保证施工顺序吗?
I have a constructor that takes some arguments. I had assumed that they were constructed in the order listed, but in one case it appears they were being constructed in reverse resulting in an abort. When I reversed the arguments the program stopped aborting. This is an example of the syntax I'm using. The thing is, a_ needs to be initialized before b_ in this case. Can you guarantee the order of construction?
例如
class A
{
public:
A(OtherClass o, string x, int y) :
a_(o), b_(a_, x, y) { }
OtherClass a_;
AnotherClass b_;
};
取决于类中成员变量声明的顺序.所以 a_ 将是第一个,然后 b_ 将是您示例中的第二个.
It depends on the order of member variable declaration in the class. So a_ will be the first one, then b_ will be the second one in your example.
这篇关于构造函数初始化列表求值顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!
fpermissive 标志有什么作用?What does the fpermissive flag do?(fpermissive 标志有什么作用?)
如何在我不想编辑的第 3 方代码中禁用来自 gccHow do you disable the unused variable warnings coming out of gcc in 3rd party code I do not wish to edit?(如何在我不想编辑的第 3 方代码中禁
使用 GCC 预编译头文件Precompiled headers with GCC(使用 GCC 预编译头文件)
如何在 OS X 中包含 omp.h?How to include omp.h in OS X?(如何在 OS X 中包含 omp.h?)
如何让 GCC 将 .text 部分编译为可写在 ELF 二进制文How can I make GCC compile the .text section as writable in an ELF binary?(如何让 GCC 将 .text 部分编译为可写在 ELF 二进制文件中?)
GCC、字符串化和内联 GLSL?GCC, stringification, and inline GLSL?(GCC、字符串化和内联 GLSL?)