使用引用类型的成员变量复制 C++ 类
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3841299/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Copying a C++ class with a member variable of reference type
提问by Mr. Boy
I've a class which stores a reference to its parent, the reference is passed in the constructor. If I try to copy an instance I get an error "error C2582: 'operator =' function is unavailable" presumably down to the reference being non-assignable.
我有一个类存储对其父级的引用,该引用在构造函数中传递。如果我尝试复制一个实例,我会收到错误“错误 C2582:'operator =' 函数不可用”,大概是由于引用不可分配。
Is there a way around this, or do I just change the variable to pointer instead of reference?
有没有办法解决这个问题,或者我只是将变量更改为指针而不是引用?
e.g (over-simplified but I think has the key points):
例如(过度简化,但我认为有关键点):
class MyClass
{
public:
MyClass(OtherClass &parent) : parent(parent) {}
private:
OtherClass &parent;
};
MyClass obj(*this);
.
.
.
obj = MyClass(*this);
采纳答案by Jerry Coffin
Yes, if you need to support assignment, making it a pointer instead of a reference is nearly your only choice.
是的,如果您需要支持赋值,将其设为指针而不是引用几乎是您唯一的选择。
回答by Tiedye
There is a way to do it and still use a reference, use a reference_wrapper
. So
有一种方法可以做到这一点并且仍然使用引用,使用reference_wrapper
. 所以
T& member;
becomes
变成
std::reference_wrapper<T> member;
Reference wrappers are basically just re-assignable references.
引用包装器基本上只是可重新分配的引用。
回答by MSN
I don't recommend this at all
我根本不推荐这个
but if you are really gung ho about doing this:
但如果你真的很想这样做:
#include <new>
MyClass::MyClass(const MyClass &rhs): parent(rhs.parent)
{
}
MyClass &MyClass::operator=(const MyClass &rhs)
{
if (this!=&rhs)
{
this->~MyClass();
new (this) MyClass(rhs);
}
return *this;
}
回答by Johannes Schaub - litb
Yes just make the member a pointer. A reference won't be able to be reseated, and there is no work-around.
是的,只是让成员成为一个指针。参考将无法重新安装,并且没有解决方法。
Edit:@"Steve Jessop" makes a valid point to how work-around the problem using the PIMPL idiom (private implementation using a "d-pointer"). In an assignment, you will delete the old implementation and create a new one copy-constructed from the source object's d-pointer.
编辑:@“Steve Jessop”有效地指出了如何使用 PIMPL 成语(使用“d 指针”的私有实现)解决问题。在分配中,您将删除旧实现并创建一个从源对象的 d 指针复制构造的新实现。
回答by florin
You need to implement a copy constructor and initialize the reference in that copy constructor, to point to the same reference as the original object.
您需要实现一个复制构造函数并在该复制构造函数中初始化引用,以指向与原始对象相同的引用。
回答by pm100
I would make it a boost::shared_ptr. You can be pretty rough with these and they take care of themselves. Whereas using a raw pointer means tha you have to worry about that object being kept alive
我会把它变成一个 boost::shared_ptr。你可以对这些很粗暴,他们会照顾好自己。而使用原始指针意味着您必须担心该对象保持活动状态
回答by jestrada
As mentioned by others, using std::reference_wrapper can be used. The helper functions std::ref() and std::cref() can be used, too. Unlike other postings, C++03 introduced reference_wrapper, ref() and cref() in the namespace std::tr1, so you have options if you're not using C++11 or beyond.
正如其他人提到的,可以使用 using std::reference_wrapper 。也可以使用辅助函数 std::ref() 和 std::cref() 。与其他帖子不同,C++03 在命名空间 std::tr1 中引入了 reference_wrapper、ref() 和 cref(),因此如果您不使用 C++11 或更高版本,您可以选择。