gotw 80 语法 - 参数列表中的初始化

发布于 2024-12-13 08:53:05 字数 607 浏览 0 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

沧笙踏歌 2024-12-20 08:53:05

看起来意图是调用 f() 并将结果分配给 B::s。之后,在调用继承的 A 构造函数时,该赋值的结果(即 s)将用作实际参数。

它在语法上是有效的。将表达式中的 s 替换为一些非成员变量,g++ 毫无问题地接受它 。您可能会看到类似的语法更常用于普通函数调用而不是构造函数调用。

It looks like the intention was to call f() and assign the result to B::s. Afterward, the result of that assignment (which is s) would be used as actual parameter when calling the inherited A 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.

別甾虛僞 2024-12-20 08:53:05

从语法上讲,它是合法的......当您有一个带有接受参数的构造函数的基类时,您当然可以传递任何表达式作为参数:

strut A {
    A(int) {}
};

struct B : A {
    B() : A( any expression that returns an int ) {}
};

问题是,在计算示例中的表达式时,该对象甚至还不是完全构造的 A 实例,因此该代码由于两个不同的原因而无效:

  1. 调用非实例的 A 方法(构造函数尚未启动): f( ) 调用是非法的。
  2. 分配给尚未初始化的成员: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:

strut A {
    A(int) {}
};

struct B : A {
    B() : A( any expression that returns an int ) {}
};

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:

  1. Calls a method of A of an non-instance (the constructor didn't start yet): f() call is illegal.
  2. Assigns to a member that hasn't been initialized: s=... is illegal.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文