C++ 禁用复制构造函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6077143/
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
Disable copy constructor
提问by Humble Debugger
I have a class :
我有一堂课:
class SymbolIndexer {
protected:
SymbolIndexer ( ) { }
public:
static inline SymbolIndexer & GetUniqueInstance ( )
{
static SymbolIndexer uniqueinstance_ ;
return uniqueinstance_ ;
}
};
How should I modify it to disable code like:
我应该如何修改它以禁用如下代码:
SymbolIndexer symbol_indexer_ = SymbolIndexer::GetUniqueInstance ( );
and only allow code like :
并且只允许像这样的代码:
SymbolIndexer & ref_symbol_indexer_ = SymbolIndexer::GetUniqueInstance ( );
回答by R. Martinho Fernandes
You can make the copy constructor private and provide no implementation:
您可以将复制构造函数设为私有并且不提供任何实现:
private:
SymbolIndexer(const SymbolIndexer&);
Or in C++11, explicitly forbid it:
或者在 C++11 中,明确禁止它:
SymbolIndexer(const SymbolIndexer&) = delete;
回答by firegurafiku
If you don't mind multiple inheritance (it is not that bad, after all), you may write simple class with private copy constructor and assignment operator and additionally subclass it:
如果您不介意多重继承(毕竟还不错),您可以编写带有私有复制构造函数和赋值运算符的简单类,并另外将其子类化:
class NonAssignable {
private:
NonAssignable(NonAssignable const&);
NonAssignable& operator=(NonAssignable const&);
public:
NonAssignable() {}
};
class SymbolIndexer: public Indexer, public NonAssignable {
};
For GCC this gives the following error message:
对于 GCC,这会给出以下错误消息:
test.h: In copy constructor ‘SymbolIndexer::SymbolIndexer(const SymbolIndexer&)':
test.h: error: ‘NonAssignable::NonAssignable(const NonAssignable&)' is private
I'm not very sure for this to work in every compiler, though. There is a related question, but with no answer yet.
不过,我不太确定这是否适用于每个编译器。有一个相关的问题,但还没有答案。
UPD:
更新:
In C++11 you may also write NonAssignable
class as follows:
在 C++11 中,您还可以编写NonAssignable
如下类:
class NonAssignable {
public:
NonAssignable(NonAssignable const&) = delete;
NonAssignable& operator=(NonAssignable const&) = delete;
NonAssignable() {}
};
The delete
keyword prevents members from being default-constructed, so they cannot be used further in a derived class's default-constructed members. Trying to assign gives the following error in GCC:
该delete
关键字防止成员被默认构造,因此它们不能在派生类的默认构造成员中进一步使用。尝试分配在 GCC 中出现以下错误:
test.cpp: error: use of deleted function
‘SymbolIndexer& SymbolIndexer::operator=(const SymbolIndexer&)'
test.cpp: note: ‘SymbolIndexer& SymbolIndexer::operator=(const SymbolIndexer&)'
is implicitly deleted because the default definition would
be ill-formed:
UPD:
更新:
Boost already has a class just for the same purpose, I guess it's even implemented in similar way. The class is called boost::noncopyable
and is meant to be used as in the following:
Boost 已经有一个用于相同目的的类,我想它甚至以类似的方式实现。该类被调用boost::noncopyable
并旨在如下使用:
#include <boost/core/noncopyable.hpp>
class SymbolIndexer: public Indexer, private boost::noncopyable {
};
I'd recommend sticking to the Boost's solution if your project policy allows it. See also another boost::noncopyable
-related questionfor more information.
如果您的项目政策允许,我建议您坚持使用 Boost 的解决方案。另请参阅另一个boost::noncopyable
相关问题以获取更多信息。
回答by Aaron Klotz
Make SymbolIndexer( const SymbolIndexer& )
private. If you're assigning to a reference, you're not copying.
让SymbolIndexer( const SymbolIndexer& )
私人。如果您要分配给引用,则您不是在复制。