1. 请设计一个类,只能在堆上创建对象
实现方式:
1.
将类的构造函数私有,拷贝构造声明成私有。防止别人调用拷贝在栈上生成对象。
2.
提供一个静态的成员函数,在该静态成员函数中完成堆对象的创建
class HeapOnly
{
public:
static HeapOnly* GetInstace()
{
return new HeapOnly;
}
HeapOnly(const HeapOnly& t) = delete;
HeapOnly operator=(const HeapOnly& t) = delete;
private:
HeapOnly()
:_a(0)
{
}
private:
int _a;
};
int main()
{
HeapOnly* p = HeapOnly::GetInstace();
cout << &p << endl;
return 0;
}
2. 请设计一个类,只能在栈上创建对象
方法一:同上将构造函数私有化,然后设计静态方法创建对象返回即可。
class StackOnly
{
public:
static StackOnly GetInstace()
{
return StackOnly();
}
void* operator new(size_t size);
void operator delete(void* p);
private:
StackOnly()
:_a(0)
{
}
private:
int _a;
};
int main()
{
//HeapOnly* p = HeapOnly::GetInstace();
//cout << &p << endl;
StackOnly::GetInstace();
//StackOnly* p = new StackOnly(StackOnly::GetInstace());
return 0;
}
3. 请设计一个类,不能被拷贝
拷贝只会放生在两个场景中:拷贝构造函数以及赋值运算符重载,因此想要让一个类禁止拷贝,只需让该类 不能调用拷贝构造函数以及赋值运算符重载即可
C++98
将拷贝构造函数与赋值运算符重载只声明不定义,并且将其访问权限设置为私有即可
原因:
1.
设置成私有:如果只声明没有设置成
private
,用户自己如果在类外定义了,就可以不能禁止拷贝
了
2.
只声明不定义:不定义是因为该函数根本不会调用,定义了其实也没有什么意义,不写反而还简
单,而且如果定义了就不会防止成员函数内部拷贝了。
C++11
扩展
delete
的用法,
delete
除了释放
new
申请的资源外,如果在默认成员函数后跟上
=delete
,表示让编译器删除掉该默认成员函数
4. 请设计一个类,不能被继承
C++98
方式
// C++98中构造函数私有化,派生类中调不到基类的构造函数。则无法继承
class NonInherit
{
public:
static NonInherit GetInstance()
{
return NonInherit();
}
private:
NonInherit()
{}
};
C++11
方法
fifinal
关键字,
fifinal
修饰类,表示该类不能被继承。
class A final
{
// ....
};
5. 请设计一个类,只能创建一个对象(单例模式)
设计模式:
设计模式(
Design Pattern
)是一套
被反复使用、多数人知晓的、经过分类的、代码设计经验的总结
。为什 么会产生设计模式这样的东西呢?就像人类历史发展会产生兵法。最开始部落之间打仗时都是人拼人的对 砍。后来春秋战国时期,七国之间经常打仗,就发现打仗也是有套路
的,后来孙子就总结出了《孙子兵 法》。孙子兵法也是类似。
使用设计模式的目的:为了代码可重用性、让代码更容易被他人理解、保证代码可靠性。 设计模式使代码编写真正工程化;设计模式是软件工程的基石脉络,如同大厦的结构一样。
单例模式:
一个类只能创建一个对象,即单例模式,该模式可以保证系统中该类只有一个实例,并提供一个访问它的全
局访问点,该实例被所有程序模块共享
。比如在某个服务器程序中,该服务器的配置信息存放在一个文件中,这些配置数据由一个单例对象统一读取,然后服务进程中的其他对象再通过这个单例对象获取这些配置 信息,这种方式简化了在复杂环境下的配置管理。
单例模式有两种实现模式:
饿汉模式
就是说不管你将来用不用,程序启动时就创建一个唯一的实例对象
如果这个单例对象在多线程高并发环境下频繁使用,性能要求较高,那么显然使用饿汉模式来避免资源竞争,提高响应速度更好。
懒汉模式
如果单例对象构造十分耗时或者占用很多资源,比如加载插件啊, 初始化网络连接啊,读取文件啊等 等,而有可能该对象程序运行时不会用到,那么也要在程序一开始就进行初始化,就会导致程序启动时非常的缓慢。 所以这种情况使用懒汉模式(延迟加载
)更好。
我们就一记录快速排序的次数来看看单例模式的两个方式
SigleTioan.hpp
#pragma once
#include<iostream>
using namespace std;
extern void Test();
//记录快排的次数
int CastConst = 0;
SigleTioan.Cpp
#include"SingleTion.hpp"
int pation(int* a, int left, int right)
{
int keyi = left;
while (left < right)
{
while (left < right && a[right] >= a[keyi])
{
--right;
}
while (left < right && a[left] <= a[keyi])
{
++left;
}
swap(a[left], a[right]);
}
swap(a[left], a[keyi]);
return left;
}
void QuickSort(int *a ,int left, int right)
{
if (left > right)
return;
++CastConst;
int key = pation(a, left, right);
QuickSort(a, left, key - 1);
QuickSort(a, key+ 1, right);
}
void Test()
{
int array[] = { 1,7,5,3,10,6,5,8,9 };
QuickSort(array, 0, 8);
for (const auto& e : array)
{
cout << e << endl;
}
}
Test.cpp
#include"SingleTion.hpp"
int main()
{
//int array[] = { 1,2,3,7,6,4,3,9,10 };
Test();
cout << CastConst << endl;
}
直接定义全局的变量在两个文件中使用,链接的时候会报错哦。这里可以如果用staic的话可以解决,但是static只作用域当前文件,在Test.cpp中CastConst还是0。只能用extern在解决
可以看出来两个文件中的变量CastConst是一个变量。当然我们也可以设计一个单例的类来实现。
#pragma once
#include<iostream>
using namespace std;
extern void Test();
//记录快排的次数
//extern int CastConst;
class Sigle
{
public:
static Sigle& GetInstance()
{
return _sig;
}
int GetConst(int n)
{
return _pConst += n;
}
int GetNum()
{
return _pConst;
}
private:
Sigle()
:_pConst(0)
{
}
private:
int _pConst;
static Sigle _sig; //创建一个静态的类
};
#include"SingleTion.hpp"
//int CastConst = 0;
Sigle Sigle::_sig; //使用前就定义好这个类,全局唯一
int pation(int* a, int left, int right)
{
int keyi = left;
while (left < right)
{
while (left < right && a[right] >= a[keyi])
{
--right;
}
while (left < right && a[left] <= a[keyi])
{
++left;
}
swap(a[left], a[right]);
}
swap(a[left], a[keyi]);
return left;
}
void QuickSort(int *a ,int left, int right)
{
if (left > right)
return;
//++CastConst;
Sigle::GetInstance().GetConst(1);
int key = pation(a, left, right);
QuickSort(a, left, key - 1);
QuickSort(a, key+ 1, right);
}
void Test()
{
int array[] = { 1,7,5,3,10,6,22,8,9 };
QuickSort(array, 0, 8);
for (const auto& e : array)
{
cout << e << endl;
}
//cout << &CastConst << endl;
}
懒汉模式
class Sigle //懒汉模式
{
public:
static Sigle& GetInstance()
{
if (_pInst == nullptr)
{
_pInst = new Sigle;
}
return *_pInst;
}
int GetConst(int n)
{
return _pConst += n;
}
int GetNum()
{
return _pConst;
}
private:
Sigle()
:_pConst(0)
{
}
private:
int _pConst;
static Sigle* _pInst;
};
#include"SingleTion.hpp"
//int CastConst = 0;
//Sigle Sigle::_sig;
Sigle* Sigle::_pInst= nullptr;
int pation(int* a, int left, int right)
{
int keyi = left;
while (left < right)
{
while (left < right && a[right] >= a[keyi])
{
--right;
}
while (left < right && a[left] <= a[keyi])
{
++left;
}
swap(a[left], a[right]);
}
swap(a[left], a[keyi]);
return left;
}
void QuickSort(int *a ,int left, int right)
{
if (left > right)
return;
//++CastConst;
Sigle::GetInstance().GetConst(1);
//sig->GetInstace()->GetConst(1);
int key = pation(a, left, right);
QuickSort(a, left, key - 1);
QuickSort(a, key+ 1, right);
}
void Test()
{
int array[] = { 1,7,5,3,10,6,22,8,9 };
QuickSort(array, 0, 8);
for (const auto& e : array)
{
cout << e << endl;
}
//cout << &CastConst << endl;
}
饿汉模式和懒汉模式的有缺点
饿汉模式的优点:简单
缺点:1.无法控制单例创建的初始化顺序。
2.如果单例对象初始化很费时间,会导致启动慢,就像卡死了
懒汉模式的有点:事先没有准备好,第一次访问才创建单例对象
缺点:相对复杂
但是懒汉模式存在线程安全问题,需要改进
#pragma once
#include<iostream>
using namespace std;
extern void Test();
//记录快排的次数
//extern int CastConst;
//class Sigle //饿汉模式
//{
//public:
// static Sigle& GetInstance()
// {
// return _sig;
// }
// int GetConst(int n)
// {
// return _pConst += n;
// }
// int GetNum()
// {
// return _pConst;
// }
//private:
// Sigle()
// :_pConst(0)
// {
//
// }
//private:
// int _pConst;
// static Sigle _sig;
//};
#include<thread>
#include<mutex>
class Sigle //懒汉模式
{
public:
static Sigle& GetInstance()
{
if (_pInst == nullptr)
{
unique_lock<mutex> lock(_mtx);
if (_pInst == nullptr)
{
_pInst = new Sigle;
}
}
return *_pInst;
}
int GetConst(int n)
{
return _pConst += n;
}
int GetNum()
{
return _pConst;
}
private:
Sigle()
:_pConst(0)
{
}
private:
int _pConst;
static Sigle* _pInst;
static mutex _mtx;
};