[英]Getting around private copy constructor/copy assignment C++
I have an object which upon initialization is passed a large data structure: 我有一个对象,在初始化时会传递一个大数据结构:
class foo {
public:
foo(const BigData& data);
command();
}
BigData
's copy constructor and copy assignment operator are private, and rightly so. BigData
的副本构造函数和副本赋值运算符是私有的,这是正确的。
However, I need to change class foo
so that command()
has access to BigData
. 但是,我需要更改
class foo
以便command()
可以访问BigData
。 Passing command(const BigData& data)
is prohibitively difficult given the structure of the program, so I want to do the following: 鉴于程序的结构,传递
command(const BigData& data)
非常困难,因此我想执行以下操作:
class foo {
public:
foo(const BigData& data);
command();
private:
BigData m_data;
}
But how can I initialize m_data
without the copy constructor or copy assignment? 但是,如何在没有复制构造函数或复制分配的情况下初始化
m_data
? I also do not like the idea of std::move
on the data
, because it needs to remain a valid object in the context of the caller. 我也不喜欢
std::move
在data
上的想法,因为它需要在调用者的上下文中保持有效的对象。
Note : I have declared the member a reference (ie const BigData& m_data
) but this does not compile under icpc: 注意 :我已经声明该成员为引用(即
const BigData& m_data
),但这不能在icpc下编译:
error: foo provides no initializer for reference member "m_data"
I have used foo(const BigData& data) : m_data(data)
as well as foo(const BigData& data) { m_data = data; }
我用过
foo(const BigData& data) : m_data(data)
以及foo(const BigData& data) { m_data = data; }
foo(const BigData& data) { m_data = data; }
. foo(const BigData& data) { m_data = data; }
。
How about storing a reference? 如何存储参考?
class foo
{
public:
foo(const BigData& data) : m_data(data) {}
command();
private:
const BigData & m_data;
};
If BigData
has no copy constructor and no assignment operator, the authors do not want you to make copies of it. 如果
BigData
没有副本构造函数和赋值运算符,那么作者不希望您为其复制副本。 However, you can make const
references to it yourself, like this: 但是,您可以自己对其进行
const
引用,如下所示:
class foo {
public:
foo(const BigData& data) : m_data(data) {}
command();
private:
const BigData &m_data;
};
Now your command()
member function has access to BigData
that has been passed to foo
's constructor. 现在,您的
command()
成员函数可以访问已传递给foo
的构造函数的BigData
。
One important point to keep in mind is that the lifetime of the BigData
object is not tied to the lifetime of the foo
object. 要记住的重要一点是,
BigData
对象的生存期与foo
对象的生存期BigData
。 You need to make sure that when foo::command()
runs, the same BigData
that has been passed to foo
's constructor is around. 您需要确保在
foo::command()
运行时,已经存在传递给foo
的构造函数的BigData
。 Otherwise, your program would have undefined behavior due to a dangling reference. 否则,由于悬空的引用,您的程序将具有未定义的行为。
Another alternative is to make your own class similar to BigData
, and make a copy from their class into yours. 另一种选择是使您自己的类类似于
BigData
,并从其类中复制一个到您的类中。 This may be prohibitive in terms of the programming effort, though. 但是,就编程工作而言,这可能是令人望而却步的。
class foo {
public:
foo(const BigData& data);
command();
private:
const BigData& m_data;
}
// constructor
foo::foo(const BigData& data)
: m_data( data )
{
// usual stuff
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.