成员初始化时使用委托构造函数 [英] Member initialization while using delegated constructor

查看:1234
本文介绍了成员初始化时使用委托构造函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开始尝试C ++ 11标准,我发现这个问题,它描述了如何从同一个类中的另一个ctor调用你的ctor,以避免使用init方法等。现在我尝试一样的代码,看起来像这样:

I've started trying out the C++11 standard and i found this question which describes how to call your ctor from another ctor in the same class to avoid having a init method or the like. Now i'm trying the same thing with code that looks like this:

hpp:

class Tokenizer
{
public:
  Tokenizer();
  Tokenizer(std::stringstream *lines);
  virtual ~Tokenizer() {};
private:
  std::stringstream *lines;
};

cpp:

Tokenizer::Tokenizer()
  : expected('=')
{
}

Tokenizer::Tokenizer(std::stringstream *lines)
  : Tokenizer(),
    lines(lines)
{
}

但是这给我一个错误:
在构造函数'config :: Tokenizer :: Tokenizer(std :: stringstream *)':
/路径/ Tokenizer.cpp:14:20:错误:'config :: Tokenizer :: lines'的mem初始化器遵循构造函数委托
我已经尝试移动Tokenizer

But this is giving me the error: In constructor ‘config::Tokenizer::Tokenizer(std::stringstream*)’: /path/Tokenizer.cpp:14:20: error: mem-initializer for ‘config::Tokenizer::lines’ follows constructor delegation I've tried moving the Tokenizer() part first and last in the list but that didn't help.

这是什么原因,我应该如何解决?我试着将行(行)移动到 this-> lines = lines; 它工作正常。

What's the reason behind this and how should i fix it? I've tried moving the lines(lines) to the body with this->lines = lines; instead and it works fine. But i would really like to be able to use the initializer list.

推荐答案

方案

当您将成员初始化委托给另一个构造函数时,假设另一个构造函数完全初始化对象,包括所有成员(即包括 lines 成员)。

When you delegate the member initialization to another constructor, there is an assumption that the other constructor initializes the object completely, including all members (i.e. including the lines member in your example). You can't therefore initialize any of the members again.

标准的相关报价是(强调我的):

The relevant quote from the Standard is (emphasis mine):


(§12.6.2/ 6)mem-initializer-list可以使用表示构造函数类本身的任何类或者decltype来委托给构造函数类的另一个构造函数。如果mem-initializer-id指定构造函数的类,它将是唯一的mem初始化;构造函数是一个委托构造函数,并且选择的构造函数是目标构造函数。 [...]

(§12.6.2/6) A mem-initializer-list can delegate to another constructor of the constructor’s class using any class-or-decltype that denotes the constructor’s class itself. If a mem-initializer-id designates the constructor’s class, it shall be the only mem-initializer; the constructor is a delegating constructor, and the constructor selected by the is the target constructor. [...]

您可以通过定义接受参数的构造函数的版本来解决这个问题

You can work-around this by defining the version of the constructor that takes arguments first:

Tokenizer::Tokenizer(std::stringstream *lines)
  : lines(lines)
{
}

,然后使用委托定义默认构造函数:

and then define the default constructor using delegation:

Tokenizer::Tokenizer()
  : Tokenizer(nullptr)
{
}

作为一般规则,您应该完全指定采用最大数量参数的构造函数版本,然后委托其他版本(使用期望的默认值作为委派中的参数)。

As a general rule, you should fully specify that version of the constructor that takes the largest number of arguments, and then delegate from the other versions (using the desired default values as arguments in the delegation).

这篇关于成员初始化时使用委托构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆