引用抽象类的类的复制和交换习惯用法
我正在尝试实现 Copy-and-Swap Idiom我的类,因为我需要实现 operator=
,并且由于它具有引用成员,并且引用只能分配一次,所以我认为上述习惯用法是一个有效的解决方法。
但现在我遇到了构建错误:
>c:\Program Files\Microsoft Visual Studio 10.0\VC\include\utility(102): error C2259: 'IVariables' : cannot instantiate abstract class
1> due to following members:
1> 'IVariables::~IVariables(void)' : is abstract
1> d:\svn.dra.workingcopy\serialport\IVariables.h(6) : see declaration of 'IVariables::~IVariables'
1> 'std::string &IVariables::operator [](const std::string &)' : is abstract
1> d:\svn.dra.workingcopy\serialport\IVariables.h(7) : see declaration of 'IVariables::operator []'
1> 'unsigned int IVariables::getVariableLength(const std::string &) const' : is abstract
1> d:\svn.dra.workingcopy\serialport\IVariables.h(8) : see declaration of 'IVariables::getVariableLength'
1> Message.cpp(32) : see reference to function template instantiation 'void std::swap<IVariables>(_Ty &,_Ty &)' being compiled
1> with
1> [
1> _Ty=IVariables
1> ]
它向我指出:
void Swap( CMessage& a, CMessage& b ){
using std::swap;
swap( a.m_bIgnoreIncoming, b.m_bIgnoreIncoming );
swap( a.m_Variables, b.m_Variables );
}
这是惯用语中的 Swap 函数,并且 m_Variables
确实是对抽象类的引用。交换这种引用是不可能的吗?如果有一个 Boost 解决方案,请告诉我,因为我最近开始使用它。
I'm trying to implement the Copy-and-Swap Idiom for my class, because I need to implement operator=
, and since it has reference members, and references can only be assigned once, I thought that the aforementioned idiom was a valid workaround.
But now I'm getting a build error:
>c:\Program Files\Microsoft Visual Studio 10.0\VC\include\utility(102): error C2259: 'IVariables' : cannot instantiate abstract class
1> due to following members:
1> 'IVariables::~IVariables(void)' : is abstract
1> d:\svn.dra.workingcopy\serialport\IVariables.h(6) : see declaration of 'IVariables::~IVariables'
1> 'std::string &IVariables::operator [](const std::string &)' : is abstract
1> d:\svn.dra.workingcopy\serialport\IVariables.h(7) : see declaration of 'IVariables::operator []'
1> 'unsigned int IVariables::getVariableLength(const std::string &) const' : is abstract
1> d:\svn.dra.workingcopy\serialport\IVariables.h(8) : see declaration of 'IVariables::getVariableLength'
1> Message.cpp(32) : see reference to function template instantiation 'void std::swap<IVariables>(_Ty &,_Ty &)' being compiled
1> with
1> [
1> _Ty=IVariables
1> ]
It points me here:
void Swap( CMessage& a, CMessage& b ){
using std::swap;
swap( a.m_bIgnoreIncoming, b.m_bIgnoreIncoming );
swap( a.m_Variables, b.m_Variables );
}
This is the Swap function from the idiom, and m_Variables
is indeed a reference to an abstract class. Is it impossible to swap that kind of references? If there is a Boost solution for this, please tell me since I recently started using it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
引用不能被交换,就像它们不能被重新分配一样。
swap
将尝试交换所引用的对象(如果使用默认的std::swap
,则通过临时对象分配它们)。由于它们引用了抽象基类,因此会失败,因为无法创建临时对象。如果您想要可重新分配的引用,请改用指针。
References can't be swapped any more than they can be reassigned.
swap
will attempt to swap the objects being referred to (by assigning them via a temporary object, if the defaultstd::swap
is used). Since they refer to an abstract base class, then this fails since the temporary can't be created.If you want a reassignable reference, then use a pointer instead.
这是一个简短而完整的示例,说明了交换对抽象类型的引用:
和输出:
Here is a short and complete example that illustrates swapping references to abstract type:
And output: