禁用复制构造函数 [英] Disable copy constructor
本文介绍了禁用复制构造函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个类:
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 ( );
推荐答案
您可以将复制构造函数设为私有, :
You can make the copy constructor private and provide no implementation:
private:
SymbolIndexer(const SymbolIndexer& that);
或在C ++ 11中,明确禁止它:
Or in C++11, explicitly forbid it:
SymbolIndexer(const SymbolIndexer& that) = delete;
这篇关于禁用复制构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文