什么是传递指针/引用到构造函数中的现有对象的首选方法? [英] What is prefered way of passing pointer/reference to existing object in a constructor?

查看:118
本文介绍了什么是传递指针/引用到构造函数中的现有对象的首选方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将从示例开始。在boost中有一个不错的tokenizer类。它接受一个字符串作为参数在构造函数中的标记:

I'll start from example. There is a nice "tokenizer" class in boost. It take a string to be tokenized as a parameter in a constructor:

std::string string_to_tokenize("a bb ccc ddd 0");
boost::tokenizer<boost::char_separator<char> > my_tok(string_to_tokenize);
/* do something with my_tok */

字符串在分词器中未修改,所以它是由const对象引用传递的。因此,我可以通过一个临时对象:

The string isn't modifed in the tokenizer, so it is passed by const object reference. Therefore I can pass a temporary object there:

boost::tokenizer<boost::char_separator<char> > my_tok(std::string("a bb ccc ddd 0"));
/* do something with my_tok */

一切都很好,但如果我尝试使用tokenizer,发生灾难。经过短时间的调查,我意识到,tokenizer类存储我给它的引用,并在进一步使用。当然,它不能很好地引用临时对象。

Everything looks fine, but if I try to use the tokenizer, a disaster occurs. After short investigation I realized, that the tokenizer class store the reference that I gave to it, and use in further use. Of course it cannot work well for reference to temporary object.

文档没有明确说明在构造函数中传递的对象将在以后使用,但是确定,它也没有说明,它不会是:)所以我不能假设这个,我的错误。

The documentation doesn't say explicitly that the object passed in the constructor will be used later, but ok, it is also not stated, that it won't be :) So I cannot assume this, my mistake.

这有点混乱。在一般情况下,当一个对象通过const引用取另一个对象时,它建议临时对象可以在那里给出。你有什么感想?这是一个不好的约定吗?也许在这种情况下应该使用指向对象(而不是引用)的指针?或者甚至更进一步 - 有一些特殊的关键字参数允许/不允许给临时对象作为参数是有用的。

It is a bit confusing however. In general case, when one object take another one by const reference, it suggest that temporary object can be given there. What do you think? Is this a bad convention? Maybe pointer to object (rather than reference) should be used in such cases? Or even further - won't it be useful to have some special keyword to argument that allow/disallow giving temporary object as parameter?

编辑:文档(版本1.49)是相当简约的,并且可能暗示这样的问题的唯一部分是:

The documentation (version 1.49) is rather minimalistic and the only part that may suggest such a problem is:


注意:在构造时实际上没有解析。解析是按需完成的,因为令牌是通过begin提供的迭代器访问的。

Note: No parsing is actually done upon construction. Parsing is done on demand as the tokens are accessed via the iterator provided by begin.

但它没有明确地陈述,将使用给定的相同对象。

But it doesn't state explicitely, that the same object that was given will be used.

但是,这个问题的观点是在这种情况下讨论编码风格,这只是一个启发我的例子。

However, the point of this question is rather discussion about coding style in such a case, this is only an example that inspired me.

推荐答案

如果某个函数(如构造函数) const然后应该

If some function (such as a constructor) takes an argument as reference-to-const then it should either


  • 清楚地记录引用对象的生命周期必须满足某些要求在此之前不会被销毁,并且发生了)


  • 如果需要在稍后使用给定对象,请在内部创建副本。

在此特殊情况下( boost :: tokenizer 类)我假设后者不是为了性能原因和/或使类可用于容器类型,甚至不可复制的第一位。因此,我认为这是一个文档错误。

In this particular case (the boost::tokenizer class) I'd assume that the latter isn't done for performance reasons and/or to make the class usable with container types which aren't even copyable in the first place. For this reason, I'd consider this a documentation bug.

这篇关于什么是传递指针/引用到构造函数中的现有对象的首选方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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