gotw 80 语法 - 参数列表中的初始化
Gotw 80 包括以下示例:
// Example 1
//
#include <string>
using namespace std;
class A
{
public:
A( const string& s ) { /* ... */ }
string f() { return "hello, world"; }
};
class B : public A
{
public:
B() : A( s = f() ) {}
private:
string s;
};
int main()
{
B b;
}
本文将讨论为什么该行 = f()
是不正确的 - 由于对象生命周期和构造顺序。文章指出,当时编译器并未发现该错误。
但是,忽略初始化顺序和对象生命周期的问题,我不明白构造函数的参数列表中的 s = f() 在语法上如何合法 - 它似乎正在尝试初始化参数列表中的成员(或者可能声明默认值)。谁能解释一下这个语法想要做什么?
Gotw 80 includes the following example:
// Example 1
//
#include <string>
using namespace std;
class A
{
public:
A( const string& s ) { /* ... */ }
string f() { return "hello, world"; }
};
class B : public A
{
public:
B() : A( s = f() ) {}
private:
string s;
};
int main()
{
B b;
}
The article goes to discuss why the line s = f()
is incorrect - due to object lifetimes and order of construction. The article states that at the time, the error wasn't picked up by the compiler.
However, ignoring the problems of order of intialisation and object lifetime, I don't see how s = f()
in the parameter list of the constructor can be legal syntactically - it appears to be trying to initialise a member in the parameter list (or perhaps declaring a default value). Can anyone explain what this syntax is trying to do?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
看起来意图是调用
f()
并将结果分配给B::s
。之后,在调用继承的A
构造函数时,该赋值的结果(即s
)将用作实际参数。它在语法上是有效的。将表达式中的
s
替换为一些非成员变量,g++ 毫无问题地接受它 。您可能会看到类似的语法更常用于普通函数调用而不是构造函数调用。It looks like the intention was to call
f()
and assign the result toB::s
. Afterward, the result of that assignment (which iss
) would be used as actual parameter when calling the inheritedA
constructor.It's syntactically valid. Replace
s
in that expression with some non-member variable, and g++ accepts it without issue. You might see similar syntax used more often with ordinary function calls instead of constructor calls.从语法上讲,它是合法的......当您有一个带有接受参数的构造函数的基类时,您当然可以传递任何表达式作为参数:
问题是,在计算示例中的表达式时,该对象甚至还不是完全构造的
A
实例,因此该代码由于两个不同的原因而无效:A
方法(构造函数尚未启动):f( )
调用是非法的。s=...
是非法的。Syntactically it's legal... when you have a base class with a constructor that takes arguments you can of course pass any expression as parameter:
The problem is that when evaluating the expression in the example the object is not even yet a fully constructed
A
instance, so that code is invalid for two distinct reasons:A
of an non-instance (the constructor didn't start yet):f()
call is illegal.s=...
is illegal.