1.设计一个只能在堆上创建对象的类
1. 将类的构造函数私有,拷贝构造声明成私有。防止别人调用拷贝在栈上生成对象。 2. 提供一个静态的成员函数,在该静态成员函数中完成堆对象的创建#include
using namespace std;class Test
{
public:static Test* CreateInstance(){return new Test;}private:Test() {}
};void test01()
{//Test t;Test* t=Test::CreateInstance();
}void main()
{test01();//system("pause");
}
2.设计一个类,只能在栈上创建对象
因为new在底层调用void* operator new(size_t size)函数,只需将该函数屏蔽掉即可。#include
using namespace std;class Test
{
public:Test() {}
private:void* operator new(size_t sz);
};void main()
{Test t;//Test* pt = new Test;
}
将构造函数私有化,然后设计静态方法创建对象返回即可。
#include
using namespace std;class StackOnly
{
public:static StackOnly CreateObject(){return StackOnly();}
private:StackOnly(const StackOnly& ) {}StackOnly() {}int m_a = 1;int m_b = 2;
};void main()
{StackOnly obj = StackOnly::CreateObject();
}
3.请设计一个类,不能被拷贝 拷贝只会放生在两个场景中:拷贝构造函数以及赋值运算符重载,因此想要让一个类禁止拷贝,只需让该类不能调用拷贝构造函数以及赋值运算符重载即可。 将拷贝构造函数与赋值运算符重载只声明不定义,并且将其访问权限设置为私有即可。 1. 设置成私有:如果只声明没有设置成private,用户自己如果在类外定义了,就可以不能禁止拷贝 了 2. 只声明不定义:不定义是因为该函数根本不会调用,定义了其实也没有什么意义,不写反而还简 单,而且如果定义了就不会防止成员函数内部拷贝了。 class CopyBan
{// ...private:CopyBan(const CopyBan&);CopyBan& operator=(const CopyBan&);//...
};
C++11扩展delete的用法,delete除了释放new申请的资源外,如果在默认成员函数后跟上=delete,表示让编译器删除掉该默认成员函数。 class CopyBan
{// ...CopyBan(const CopyBan&)=delete;CopyBan& operator=(const CopyBan&)=delete;//...
};
4.设计一个类,不能被继承
1.构造函数私有
#include
using namespace std;class Base
{
private:Base();
};class D :public Base
{};void main()
{D d;
}
2.final关键字
#include
using namespace std;class Base final
{};class D :public Base
{};void main()
{D d;
}
5.单例模式
一个类只能创建一个对象,即单例模式,该模式可以保证系统中该类只有一个实例,并提供一个访问它的全局访问点,该实例被所有程序模块共享。比如在某个服务器程序中,该服务器的配置信息存放在一个文件 中,这些配置数据由一个单例对象统一读取,然后服务进程中的其他对象再通过这个单例对象获取这些配置 信息,这种方式简化了在复杂环境下的配置管理。 懒汉模式: 如果单例对象构造十分耗时或者占用很多资源,比如加载插件啊, 初始化网络连接啊,读取文件啊等等,而有可能该对象程序运行时不会用到,那么也要在程序一开始就进行初始化,就会导致程序启动时 非常的缓慢。 所以这种情况使用懒汉模式(延迟加载)更好。 注意这里通过双检锁实现了线程安全同时提高了效率。#include
#include
#include
#include
using namespace std;
//单例模式 工厂模式 抽象工厂模式 迭代器模式 观察者模式 MVC
//这个代码非线程安全#define N 1500
mutex mt;class Singleton
{
public:static Singleton* Instance(){//双检锁if (_instance == nullptr){mt.lock();if (_instance == nullptr){Sleep(100);//人工干预_instance = new Singleton;}mt.unlock();}return _instance;}
protected:Singleton(){}
private:static Singleton* _instance;
};Singleton* Singleton::_instance = nullptr;void thread_fun()
{Singleton* ps = Singleton::Instance();cout << ps << endl;
}void main()
{//Singleton* pst1 = Singleton::Instance();//Singleton* pst2 = Singleton::Instance();//Singleton* pst3 = Singleton::Instance();//Singleton* pst4 = Singleton::Instance();//Singleton* pst5 = Singleton::Instance();Singleton st;//cout << "OK" << endl;thread* th[N];//for (int i = 0; i < 10; ++i)//{// thread th(thread_fun);// //th.join();//每个线程结束再执行//}for (int i = 0; i < N; ++i){th[i] = new thread(thread_fun);}for (int i = 0; i < N; ++i){th[i]->join();}
}//class Test
//{};
//
//void main()
//{
// Test t1;
// Test t2;
//}
饿汉模式:就是说不管你将来用不用,程序启动时就创建一个唯一的实例对象。
如果这个单例对象在多线程高并发环境下频繁使用,性能要求较高,那么显然使用饿汉模式来避免资源竞争,提高响应速度更好。#include
using namespace std;class Test
{Test* pt;//只能定义自身类型指针//Test t;//不能创建自身类型对象static Test t;//不属于这个类,在主函数之前产生 唯一方法
};// 饿汉模式
// 优点:简单
// 缺点:可能会导致进程启动慢,且如果有多个单例类对象实例启动顺序不确定。
//只要程序运行,都会产生对象
class Singleton
{
public:static Singleton* GetInstance(){return &m_instance;}private:// 构造函数私有Singleton() {};// C++98 防拷贝//Singleton(Singleton const&);//Singleton& operator=(Singleton const&);orC++11//Singleton(Singleton const&) = delete;//Singleton& operator=(Singleton const&) = delete;static Singleton m_instance;int m_a;int m_b;
};Singleton Singleton::m_instance; // 在程序入口之前就完成单例对象的初始化void main()
{Singleton* ps1 = Singleton::GetInstance();Singleton* ps2 = Singleton::GetInstance();Singleton* ps3 = Singleton::GetInstance();Singleton* ps4 = Singleton::GetInstance();Singleton* ps5 = Singleton::GetInstance();return;
}
单例类模板
#include
#include
using namespace std;template
class LASingletonTemplateBase
{
private:static T* sm_instance;
protected:LASingletonTemplateBase(){assert(sm_instance == 0);sm_instance = static_cast(this);}virtual ~LASingletonTemplateBase(){assert(sm_instance != 0);sm_instance = 0;}
public:static T* get_instance_ptr(){if (sm_instance == 0){sm_instance = new T();}return sm_instance;}static T& get_instance_ref(){if (sm_instance == 0){sm_instance = new T();}return *sm_instance;}static void remove_instance(){assert(sm_instance);if (sm_instance){delete sm_instance;}assert(sm_instance == 0);}
};
template
T* LASingletonTemplateBase::sm_instance = 0;
//
class Test
{};void main()
{Test* pt1 = LASingletonTemplateBase::get_instance_ptr();Test* pt2 = LASingletonTemplateBase::get_instance_ptr();Test* pt3 = LASingletonTemplateBase::get_instance_ptr();Test& rt1 = LASingletonTemplateBase::get_instance_ref();Test& rt2 = LASingletonTemplateBase::get_instance_ref();
}
boost库的一个小功能--防止拷贝的类
#include
#include
using namespace std;
using namespace boost;class Test:public noncopyable//禁止拷贝构造和赋值的类
{};void main()
{Test t1;//Test t2=t1;Test t2;//t2 = t1;
}
在C语言中,如果赋值运算符左右两侧类型不同,或者形参与实参类型不匹配,或者返回值类型与接收返回值类型不一致时,就需要发生类型转化,C语言中总共有两种形式的类型转换:隐式类型转换和显式类型转换。 1. 隐式类型转化:编译器在编译阶段自动进行,能转就转,不能转就编译失败 2. 显式类型转化:需要用户自己处理 缺陷: 转换的可视性比较差,所有的转换形式都是以一种相同形式书写,难以跟踪错误的转换 static_cast static_cast用于非多态类型的转换(静态转换),编译器隐式执行的任何类型转换都可用static_cast,但它 不能用于两个不相关的类型进行转换 。 void test01()
{double d = 3.14;int i = static_cast(d);double* pb = &d;//int a = static_cast(pb);//不安全int a = (int)pb;int* pa = (int*)pb;//int* pa = static_cast(pb);//不安全
}
const_cast const_cast最常用的用途就是删除变量的const属性,方便赋值 void test02()
{const int a = 10;const int* ptr = &a;int* ptr1 = (int*)&a;int* ptr2 = const_cast (&a);double* pb = (double*)&a;//double* pb1 = const_cast (&a);//仅针对相同类型,不能跨类型
}
reinterpret_cast 使用场景之一:多继承中转换子类指针为父类指针,并使其指向对象首地址 class A
{
private:int m_a = 1;
};class B
{
private:int m_b = 2;
};class C:public A,public B
{
private:int m_c = 3;
};void test03()
{C c;//cout << "&c=" << &c << endl;A* pa = &c;//cout << "pa=" << pa << endl;B* pb = &c;//cout << "pb=" << pb << endl;//printf("&c=%p,pa=%p,pb=%p\n", &c, pa, (A*)pb);//printf("&c=%p,pa=%p,pb=%p\n", &c, pa, reinterpret_cast(pb));printf("&c=%p,pa=%p,pb=%p\n", &c, (A*)&c, (B*)&c);printf("&c=%p,pa=%p,pb=%p\n", &c, (A*)&c, reinterpret_cast(&c));
}
dynamic_cast
dynamic_cast用于将一个父类对象的指针/引用转换为子类对象的指针或引用(动态转换) 向上转型:子类对象指针/引用->父类指针/引用(不需要转换,赋值兼容规则) 向下转型:父类对象指针/引用->子类指针/引用(用dynamic_cast转型是安全的) 注意: 1. dynamic_cast只能用于含有虚函数的类 2. dynamic_cast会先检查是否能转换成功,能成功则转 换,不能则返回0. 注意:父类指针必须指向子类对象(即符合多态原则)才能转换成功。#include
using namespace std;class A
{
public:virtual void fun(){cout << "This is A::fun()" << endl;}
};class B :public A
{
public:virtual void fun(){cout << "This is B::fun()" << endl;}void h(){cout << "This is B::h()" << endl;}int m_b = 10;
};void Active(A* pa)
{pa->fun();//B* pb = (B*)pa;B* pb = dynamic_cast(pa);if (pb != nullptr){pb->h();pb->m_b = 20;}else{cout << "pb=nullptr" << endl;}
};void main()
{B b;A a;Active(&a);
}
RTTI
RTTI:Run-time Type identifification的简称,即:运行时类型识别。 C++通过以下方式来支持RTTI: 1. typeid运算符 2. dynamic_cast运算符