构造函数的首选参数传递 [英] Preferred parameter passing for constructors
问题描述
有传递构造函数参数的首选做法吗?特别是如果这些构造函数参数用于初始化成员变量。
一个简化的例子。
class Example
{
public:
示例(/ * type-1 * / str,/ * type-2 * / v):
m_str str),
m_v(v)
{}
/ *其他方法* /
private:
std :: string m_str ;
std :: complex< float> m_v;
};
选项为:
- 传递值,然后
std :: move
将对象加入成员。 -
const&
,然后将参数复制到成员中。 -
&&
我的默认/首选参数传递方式应该是什么?
更改不同的参数类型?
我的直觉说使用右值引用,但我不知道我理解所有的优点和缺点。
选项1:
class示例
{
public:
示例(std :: string str,const std :: complex< float>& v):
m_str (str)),
m_v(v)
{}
/ *其他方法* /
private:
std :: string m_str;
std :: complex< float> m_v;
};
这有很好的性能,很容易编写代码。一个地方下降一点点最佳是当绑定一个左值到 str
。在这种情况下,您将执行复制构造和移动构建。最佳的只是一个复制结构。注意,一个 std :: string
的移动结构应该非常快。
然而,如果你真的需要将最后一个循环的性能提高,你可以这样做:
选项2:
class Example
{
public:
示例(const std :: string& str,const std :: complex< float>& v):
m_str(str),
m_v(v)
{}
示例(std :: string&& str,const std :: complex< float>& v):
m_str(std :: move(str)),
m_v(v)
{}
/ *其他方法* /
private:
std :: string m_str;
std :: complex< float> m_v;
};
此选项的主要缺点是必须重载/复制构造函数逻辑。事实上,如果你需要在 const&
和&& $之间重载的一个或两个参数,这个公式将变得不现实c $ c>。
Is there a preferred practice for passing constructor parameters? In particular if those constructor parameters are used to initialize member variables.
A simplified example.
class Example
{
public:
Example( /*type-1*/ str, /*type-2*/ v ):
m_str( str ),
m_v( v )
{ }
/* other methods */
private:
std::string m_str;
std::complex<float> m_v;
};
The options are:
- pass-by-value, then
std::move
the object into the member. const&
, then copy the parameter into the member.&&
, then initialize the member with the parameter.
What should be my default/preferred parameter passing style?
Does it change with different parameter types?
My intuition says use rvalue-references, but I'm not sure I understand all the pros and cons.
Option 1:
class Example
{
public:
Example( std::string str, const std::complex<float>& v ):
m_str( std::move(str) ),
m_v( v )
{ }
/* other methods */
private:
std::string m_str;
std::complex<float> m_v;
};
This has pretty good performance and is easy to code. The one place it falls a little short of the optimum is when you bind an lvalue to str
. In this case you execute both a copy construction and a move construction. The optimum is only a copy construction. Note though that a move construction for a std::string
should be very fast. So I would start with this.
However if you really need to pull the last cycles out of this for performance you can do:
Option 2:
class Example
{
public:
Example( const std::string& str, const std::complex<float>& v ):
m_str( str ),
m_v( v )
{ }
Example( std::string&& str, const std::complex<float>& v ):
m_str( std::move(str) ),
m_v( v )
{ }
/* other methods */
private:
std::string m_str;
std::complex<float> m_v;
};
The main disadvantage of this option is having to overload/replicate the constructor logic. Indeed this formula will become unrealistic if you have more than one or two parameters that you need to overload between const&
and &&
.
这篇关于构造函数的首选参数传递的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!