1.设计一个类,不可以被拷贝
class BanCopy
{
public:
BanCopy()
{}
private:
BanCopy(const BanCopy&);
BanCopy& operator=(const BanCopy&);
};
c++98中,我们可以将拷贝构造函数和赋值运算符重载函数只声明不定义,并且将其访问权限设置为私有。
1.声明成私有可以防止在类外面实现
2.只声明不定义:不定义是因为该函数根本不会调用,定义了也没有意义。
缺陷:可能会在类内实现
c++11的做法
class BanCopy
{
BanCopy(const BanCopy&) = delete;
BanCopy& operator=(const BanCopy&) = delete;
};
2.请设计一个类,只能在堆上创建对象
- 将类的构造函数和拷贝构造函数都声明为私有。
- 提供一个静态成员函数,在该静态成员函数中完成堆对象的创建
class HeapOnly
{
public:
static HeapOnly* getObj()
{
return new HeapOnly;
}
private:
HeapOnly()
{}
HeapOnly(const HeapOnly&) = delete;
};
int main()
{
HeapOnly* hp = HeapOnly::getObj();
return 0;
}
3.设计一个类,只能在栈上面创建对象
- 将构造函数私有化
- 设计静态方法返回创建的对象
class StackOnly
{
public:
static StackOnly GetObj()
{
return StackOnly();
}
void* operator new(size_t size) = delete;
void operator delete(void* p) = delete;
private:
StackOnly()
:_a(0)
{}
int _a;
};
设计一个类,不能被继承
class StopInherit
{
public:
static StopInherit getObj()
{
return StopInherit();
}
private:
StopInherit()
{}
};
5.设计一个类,只能创建一个对象(单例模式)
设计模式:
设计模式(Design Pattern)是一套被反复使用、多人知晓、经过分类的、代码设计经验的总结。
单例模式:
一个类只能创建一个对象,即单例模式,该模式可以保证系统中该类只有一个实例,并提供一个访问它的全局访问点,该实例被所有程序模块共享。
比如在某个服务器程序中,该服务器的配置信息放在一个文件中,这些配置数据由一个单例对象统一读取,然后服务进程中的其他对象再通过这个单例对象获取这些配置信息,这种方式简化了在复杂环境下的配置管理。
两种实现模式:
-
饿汉模式
不管将来用不用,程序启动时就创建一个唯一的实例对象。
class Singleton
{
public:
static Singleton* GetInstance()
{
return _spInst;
}
private:
Singleton()
{}
Singleton(const Singleton&) = delete;
Singleton& operator=(const Singleton &) = delete;
static Singleton* _spInst;
int _a = 0;
};
Singleton* Singleton::_spInst = new Singleton;
优点: 简单
缺点:有多个单例类对象实例启动的顺序无法确定,初始化启动进程慢。
- 懒汉模式(延迟加载)
class InfoMgr
{
public:
static InfoMgr* GetInstance()
{
if (_spInst == nullptr)
{
_spInst = new InfoMgr;
}
return _spInst;
}
void SetAddress(const string& s)
{
_address = s;
}
string& GetAddress()
{
return _address;
}
//.实现一个内嵌的垃圾回收类
class CGarbo
{
public:
~CGarbo()
{
if (_spInst)
delete _spInst;
}
};
static CGarbo Garbo;
private:
InfoMgr()
{}
~InfoMgr()
{
//假设析构时需要信息写道文件持久化
}
InfoMgr(const InfoMgr&) = delete;
string _address;
int _secretKey;
static InfoMgr* _spInst;
};
InfoMgr* InfoMgr::_spInst = nullptr;
InfoMgr::CGarbo Garbo;