禁用复制构造函数
我有一个类:
class SymbolIndexer {
protected:
SymbolIndexer ( ) { }
public:
static inline SymbolIndexer & GetUniqueInstance ( )
{
static SymbolIndexer uniqueinstance_ ;
return uniqueinstance_ ;
}
};
我应该如何修改它以禁用如下代码:
SymbolIndexer symbol_indexer_ = SymbolIndexer::GetUniqueInstance ( );
并仅允许如下代码:
SymbolIndexer & ref_symbol_indexer_ = SymbolIndexer::GetUniqueInstance ( );
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 ( );
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以将复制构造函数设为私有并且不提供任何实现:
或者在 C++11 中明确禁止它:
You can make the copy constructor private and provide no implementation:
Or in C++11, explicitly forbid it:
如果您不介意多重继承(毕竟,这并没有那么糟糕),您可以使用私有复制构造函数和赋值运算符编写简单的类,并另外对其进行子类化:
对于 GCC,这会给出以下错误消息:
我不是很不过,这肯定适用于每个编译器。有一个相关问题,但尚未得到答案。
UPD:
在 C++11 中,您还可以编写
NonAssignable
类,如下所示:delete
关键字可防止默认构造成员,因此它们不能在派生类的默认构造成员中进一步使用。尝试分配会在 GCC 中出现以下错误:UPD:
Boost 已经有一个用于相同目的的类,我猜它甚至以类似的方式实现。该类名为
boost: :noncopyable
的用途如下:如果您的项目策略允许,我建议坚持使用 Boost 的解决方案。另请参阅另一个
boost::noncopyable
相关问题了解更多信息。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:
For GCC this gives the following error message:
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: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: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:I'd recommend sticking to the Boost's solution if your project policy allows it. See also another
boost::noncopyable
-related question for more information.将
SymbolIndexer( const SymbolIndexer& )
设为私有。如果您分配给参考,那么您就不是在复制。Make
SymbolIndexer( const SymbolIndexer& )
private. If you're assigning to a reference, you're not copying.