实现一个单例
class Singleton {
public:
static Singleton& Instance() {
static Singleton instance;
return instance;
}
Singleton(Singleton const&) = delete;
Singleton(Singleton&&) = delete;
Singleton& operator=(Singleton const&) = delete;
Singleton& operator=(Singleton &&) = delete;
protected:
Singleton() = default;
~Singleton() = default;
}
可以使用下面的宏简化代码实现,禁止类的拷贝和移动构造
#define DISALLOW_COPY(ClassName) \
ClassName(const ClassName&) = delete; \
ClassName& operator=(const ClassName&) = delete
#define DISALLOW_MOVE(ClassName) \
ClassName(ClassName&&) = delete; \
ClassName& operator=(ClassName&&) = delete
#define DISALLOW_COPY_AND_MOVE(ClassName) \
DISALLOW_COPY(ClassName); \
DISALLOW_MOVE(ClassName)
我们定义一个ClassA的类,可以使用上面的宏来简化编程
class ClassA final {
public:
ClassA(){}
~ClassA() {}
DISALLOW_COPY_AND_MOVE(ClassA);
void Reset();
void CopyFrom(const ClassA* src);
void Swap(ClassA* other);
private:
void* buffer_;
size_t buffer_size_;
};