【面试练习题】单例模式

饿汉式,基本版

#include <iostream>

using namespace std;
class Single {
private:
	Single() {} // 构造函数私有
	Single(const Single &) {}; // 拷贝构造函数私有
	Single& operator=(const Single&) {}; // 赋值操作符私有,不允许复制操作
	static Single _obj; // 一个属于类的对象
	static int a; // 该对象的其他数据
	
public:
	virtual ~Single() {}
	static Single get_obj(){
		return _obj;
	}
	static int get_data() {
		return a;
	}
	static void set_data(int a2) {
		a = a2;
	}
};
Single Single::_obj;// 类static成员变量必须在类外初始化,否则会出现连接错误,可能是RAII
int Single::a = 1;// 为什么静态成员不能再类内初始化:因为静态成员属于整个类,而不属于某个对象,如果在类内初始化,会导致每个对象都包含该静态成员,这是矛盾的。

int main()
{
	Single::get_obj().set_data(12);
	cout << Single::get_obj().get_data() << endl;
	return 0;
}

或者:

#include <iostream>

using namespace std;
class Single {
private:
	Single() {} // 构造函数私有
	Single(const Single &) {}; // 拷贝构造函数私有
	Single& operator=(const Single&) {}; // 赋值操作符私有,不允许复制操作
	static Single* _obj; // 指向属于类的对象
	static int *a;// 该对象的其他数据
	
public:
	virtual ~Single() {}
	static Single* get_obj(){
		return _obj;
	}
	static int get_data() {
		return *a;
	}
	static void set_data(int a2) {
		*a = a2;
	}
};

Single* Single::_obj = new Single;
int* Single::a = new int(0);

int main()
{
	Single::get_obj()->set_data(12);
	cout << Single::get_obj()->get_data() << endl;
	return 0;
}

加入了模板的饿汉式:

#include <iostream>

using namespace std;

template<typename T>
class Single {
private:
	Single() {} // 构造函数私有
	Single(const Single &) {}; // 拷贝构造函数私有
	Single& operator=(const Single&) {}; // 赋值操作符私有,不允许赋值操作
	static Single<T>* _obj; // 指向属于类的对象
	static T* a;// 该对象的其他数据
	
public:
	virtual ~Single() {}
	static Single<T>* get_obj(){
		return _obj;
	}
	static T get_data() {
		return *a;
	}
	static void set_data(T a2) {
		*a = a2;
	}
};

template<typename T>
Single<T>* Single<T>::_obj = new Single<T>;

template<typename T>
T* Single<T>::a = new T;

int main()
{
	Single<int>::get_obj()->set_data(12);
	cout << Single<int>::get_obj()->get_data() << endl;
	return 0;
}

精简版:

#include <iostream>

using namespace std;

template<typename T>
class Single {
private:
	Single() {} // 构造函数私有
	Single(const Single &) {}; // 拷贝构造函数私有
	Single& operator=(const Single&) {}; // 赋值操作符私有,不允许赋值操作
	static T* a;// 该对象的其他数据

public:
	virtual ~Single() {}
	static T* get_obj() {
		return a;
	}
};

template<typename T>
T* Single<T>::a = new T;

int main()
{
	*(Single<int>::get_obj()) = 12;
	cout << *(Single<int>::get_obj()) << endl;
	return 0;
}

懒汉模式:

#include <iostream>
#include <mutex>

using namespace std;

template<typename T>
class Single {
private:
	Single() {} // 构造函数私有
	Single(const Single &) {}; // 拷贝构造函数私有
	Single& operator=(const Single&) {}; // 赋值操作符私有,不允许赋值操作
	static T* _point;
	static mutex _mutex;
public:
	virtual ~Single() {}
	static T* get_obj(){
		if (_point == NULL) {
			_mutex.lock();
				if (_point == NULL) {
					_point = new T;
				}
			_mutex.unlock();
		}
		return _point;
	}
};


template<typename T>
T* Single<T>::_point = new T;

template<typename T>
mutex Single<T>::_mutex;


int main()
{
	*(Single<int>::get_obj()) = 12;
	cout << *(Single<int>::get_obj()) << endl;
	return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值