c++修炼之路之特殊类设计与类型转换

目录

 一:特殊类设计

1.设计一个不能被拷贝的类

2.设计一个只能在堆上创建对象的类 

3.设计一个只能在栈上创建对象的类 

4.设计一个不能被继承的类 

5.设计一个只能创建一个对象的类(单例模式) 

二:c++的类型转换 

1.自定义类型,内置类型的各种转换

2.C++强制类型转换 

3.RTTI

接下来的日子会顺顺利利,万事胜意,生活明朗-----------林辞忧 

 一:特殊类设计

1.设计一个不能被拷贝的类

拷贝只会发生在两个场景中:拷贝构造函数以及赋值运算符重载,因此想要让一个类禁止拷贝,
只需让该类不能调用拷贝构造函数以及赋值运算符重载即可

在c++98中将拷贝构造函数与赋值运算符重载只声明不定义,并且将其访问权限设置为私有

class CopyBan
{
	// ...

private:
	CopyBan(const CopyBan&);
	CopyBan& operator=(const CopyBan&);
	//...
};

原因:
1. 设置成私有:如果只声明没有设置成private,用户自己如果在类外定义了,就可以不
能禁止拷贝了
2. 只声明不定义:不定义是因为该函数根本不会调用,定义了其实也没有什么意义,不写
反而还简单,而且如果定义了就不会防止成员函数内部拷贝了

在c++11后,在默认成员函数后跟上=delete,表示让编译器删除掉该默认成员函数 

class CopyBan
{
  // ...
  CopyBan(const CopyBan&)=delete;
  CopyBan& operator=(const CopyBan&)=delete;
  //...
};

2.设计一个只能在堆上创建对象的类 

实现方式1:
1. 将类的构造函数私有,拷贝构造声明成私有。防止别人调用拷贝在栈上生成对象。
2. 提供一个静态的成员函数,在该静态成员函数中完成堆对象的创建

class HeapOnly
{
public:
	static HeapOnly* CreateObj()
	{
		return new HeapOnly;
	}

	HeapOnly(const HeapOnly& hp) = delete;
	HeapOnly& operator=(const HeapOnly& hp) = delete;
private:
	HeapOnly()
	{}
};

int main()
{
	//三种创建对象的方式
	/*HeapOnly hp1;
	static HeapOnly hp2;
	HeapOnly* hp3 = new HeapOnly;*/

	HeapOnly* hp4 = HeapOnly::CreateObj();

	//但是此时防不住这样创建对象的,因此将拷贝构造和赋值给禁掉的
	//HeapOnly hp5(*hp4);
	delete hp4;

	return 0;
}

实现方式2:将类的析构函数私有

class HeapOnly
{
public:
	void Destroy()
	{
		delete this;
	}
private:
	~HeapOnly()
	{}
};

int main()
{
	//此时自定义类型是会自动调用构造和析构的,指针不会
	//HeapOnly hp1;
	//static HeapOnly hp2;
	HeapOnly* hp3 = new HeapOnly;

	//此时不能直接delete hp3
	hp3->Destroy();
	return 0;
}

3.设计一个只能在栈上创建对象的类 

方式1:不禁用拷贝构造

class StackOnly
{
public:
	static StackOnly CreateObj()
	{
		return StackOnly();
	}

	//直接禁用拷贝构造的话,上面的返回是要经过拷贝构造的
	//StackOnly(const StackOnly& s) = delete;
	void* operator new(size_t size) = delete;
	void operator delete(void* p) = delete;
private:
	StackOnly()
		:_a(0)
	{}
private:
	int _a;
};

int main()
{
	//static StackOnly s1;
	//StackOnly s2;
	//StackOnly* s3 = new StackOnly;

	StackOnly s4 = StackOnly::CreateObj();

	// 禁掉operator new可以把下面用new 调用拷贝构造申请对象给禁掉
	//StackOnly* s5 = new StackOnly(s4);

	//但还是防不住这种情况
	static StackOnly s6(s4);

	return 0;
}

方式2:禁用拷贝构造,提供移动构造

class StackOnly
{
public:
	static StackOnly CreateObj()
	{
		return StackOnly();
	}

	StackOnly(StackOnly&& s)
	{}

	StackOnly(const StackOnly& s) = delete;
private:
	StackOnly()
		:_a(0)
	{}
private:
	int _a;
};

int main()
{
	//static StackOnly s1;
	//StackOnly s2;
	//StackOnly* s3 = new StackOnly;

	StackOnly s4 = StackOnly::CreateObj();

	//此时拷贝构造的情况就都禁用了
	//StackOnly* s5 = new StackOnly(s4);
	//static StackOnly s6(s4);

	// 但是防不住下面的
	static StackOnly s6(move(s4));
	StackOnly* s5 = new StackOnly(move(s6));

	return 0;
}

4.设计一个不能被继承的类 

// C++98中构造函数私有化,派生类中调不到基类的构造函数。则无法继承
class NonInherit
{
public:
	static NonInherit GetInstance()
	{
		return NonInherit();
	}
private:
	NonInherit()
	{}
};


//C++11final关键字,final修饰类,表示该类不能被继承
class A  final
{
  // ....
};

5.设计一个只能创建一个对象的类(单例模式) 

设计模式(Design Pattern)是一套被反复使用、多数人知晓的、经过分类的、代码设计经验的
总结

单例模式:一个类只能创建一个对象,即单例模式,该模式可以保证系统中该类只有一个实例,并提供一个访问它的全局访问点,该实例被所有程序模块共享

单例模式实现模式1:饿汉模式

// 饿汉模式   在main函数之前创建对象
// 1、多个饿汉模式的单例,某个对象初始化内容较多(读文件),会导致程序启动慢
// 2、A和B两个饿汉,对象初始化存在依赖关系,要求A先初始化,B再初始化,饿汉无法保证
class InfoMgr
{
public:
	static InfoMgr& GetInstance()
	{
		return _ins;
	}

	void Print()
	{
		cout << _ip << endl;
		cout << _port << endl;
		cout << _buffSize << endl;
	}
private:
	InfoMgr(const InfoMgr&) = delete;
	InfoMgr& operator=(const InfoMgr&) = delete;

	InfoMgr()
	{
		cout << "InfoMgr()" << endl;
	}
private:
	string _ip = "127.0.0.1";
	int _port = 80;
	size_t _buffSize = 1024 * 1024;
	//...
	static InfoMgr _ins;
};

InfoMgr InfoMgr::_ins;

int main()
{
	InfoMgr::GetInstance().Print();
	//InfoMgr copy(InfoMgr::GetInstance());

	return 0;
}

单例模式实现模式2:懒汉模式(c++11之前)

// 懒汉模式
class InfoMgr
{
public:
	static InfoMgr& GetInstance()
	{
		// 第一次调用时创建单例对象
		// 线程安全的风险
		if (_pins == nullptr)
		{
			_pins = new InfoMgr;
		}

		return *_pins;
	}

	void Print()
	{
		cout << _ip << endl;
		cout << _port << endl;
		cout << _buffSize << endl;
	}

	static void DelInstance()
	{
		delete _pins;
		_pins = nullptr;
	}

private:
	InfoMgr(const InfoMgr&) = delete;
	InfoMgr& operator=(const InfoMgr&) = delete;

	InfoMgr()
	{
		cout << "InfoMgr()" << endl;
	}
private:
	string _ip = "127.0.0.1";
	int _port = 80;
	size_t _buffSize = 1024 * 1024;
	//...

	static InfoMgr* _pins;
};

InfoMgr* InfoMgr::_pins = nullptr;

int main()
{
	InfoMgr::GetInstance().Print();
	
	return 0;
}

单例模式实现模式2:懒汉模式(c++11之后)

class InfoMgr
{
public:
	static InfoMgr& GetInstance()
	{
		// 第一次调用时创建单例对象
		static InfoMgr ins;
		return ins;
	}

	void Print()
	{
		cout << _ip << endl;
		cout << _port << endl;
		cout << _buffSize << endl;
	}
private:
	InfoMgr(const InfoMgr&) = delete;
	InfoMgr& operator=(const InfoMgr&) = delete;

	InfoMgr()
	{
		cout << "InfoMgr()" << endl;
	}
private:
	string _ip = "127.0.0.1";
	int _port = 80;
	size_t _buffSize = 1024 * 1024;
	//...
};

int main()
{
	InfoMgr::GetInstance().Print();
	
	return 0;
}

二:c++的类型转换 

发生类型转换的,类型之间是有一定关联的

1.自定义类型,内置类型的各种转换

// a、内置类型之间
// 1、隐式类型转换    整形之间/整形和浮点数之间
// 2、显示类型的转换  指针和整形、指针之间

int main()
{
	int i = 1;
	// 隐式类型转换
	double d = i;
	printf("%d, %.2f\n", i, d);

	int* p = &i;
	// 显示的强制类型转换
	int address = (int)p;
	printf("%p, %d\n", p, address);

	return 0;
}
// b、内置类型和自定义类型之间
// 1、自定义类型 = 内置类型  ->构造函数支持
// 2、内置类型 = 自定义类型  ->使用operator+类型 支持
class A
{
public:
	//explicit A(int a)
	A(int a)
		:_a1(a)
		,_a2(a)
	{}

	A(int a1, int a2)
		:_a1(a1)
		, _a2(a2)
	{}

	// ()被仿函数占用了,不能用
	// operator 类型实现,无返回类型
 	//explicit operator int()
	operator int()
	{
		return _a1 + _a2;
	}
private:
	int _a1 = 1;
	int _a2 = 1;
};
int main()
{
	//单参数/多参数构造函数会发生隐式类型转换,产生具有常性的临时变量
	string s1 = "1111111";
	A aa1 = 1;
	//A aa1 = (A)1;
	A aa2 = { 2,2 };
	const A& aa3 = { 2,2 };

	int z = aa1.operator int();
	//int x = (int)aa1;
	int x = aa1;
	int y = aa2;
	cout << x << endl;
	cout << y << endl;

	std::shared_ptr<int> foo;
	std::shared_ptr<int> bar(new int(34));

	//if (foo.operator bool())
	if (foo)
		std::cout << "foo points to " << *foo << '\n';
	else 
		std::cout << "foo is null\n";

	if (bar)
		std::cout << "bar points to " << *bar << '\n';
	else
		std::cout << "bar is null\n";

	return 0;
}
// c、自定义类型和自定义类型之间 -- 对应的构造函数支持
class A
{
public:
	A(int a)
		:_a1(a)
		, _a2(a)
	{}

	A(int a1, int a2)
		:_a1(a1)
		, _a2(a2)
	{}

	int get() const
	{
		return _a1 + _a2;
	}
private:
	int _a1 = 1;
	int _a2 = 1;
};

class B
{
public:
	B(int b)
		:_b1(b)
	{}

	B(const A& aa)
		:_b1(aa.get())
	{}

private:
	int _b1 = 1;
};

int main()
{
	A aa1(1);
	B bb1(2);

	bb1 = aa1;
	B& ref1= bb1;
	const B& ref2 = aa1;

	return 0;
}

这里还有个在list中使用const迭代器遍历的场景 

 

2.C++强制类型转换 

标准C++为了加强类型转换的可视性,引入了四种命名的强制类型转换操作符:
static_cast、reinterpret_cast、const_cast、dynamic_cast

int main()
{
	// 对应隐式类型转换 -- 数据的意义没有改变
	double d = 12.34;
	int a = static_cast<int>(d);
	cout << a << endl;
	
	// 对应强制类型转换 -- 数据的意义已经发生改变
	int* p1 = reinterpret_cast<int*>(a);

	// 对应强制类型转换中有风险的去掉const属性
	volatile const int b = 2;
	int* p2 = const_cast<int*>(&b);
	*p2 = 3;

	cout << b << endl;
	cout << *p2 << endl;

	return 0;
}

class A
{
public:
	virtual void f() {}

	int _a = 1;
};

class B : public A
{
public:
	int _b = 2;
};

//void fun(A* pa)
//{
//	// dynamic_cast会先检查是否能转换成功,能成功则转换,不能则返回
//	// 指向父类转换时有风险的,后续访问存在越界访问的风险
//	// 指向子类转换时安全
//	B* pb1 = (B*)pa;
//	cout << "pb1:" << pb1 <<endl;
//	cout << pb1->_a << endl;
//	cout << pb1->_b << endl;
//	pb1->_a++;
//	pb1->_b++;
//	cout << pb1->_a << endl;
//	cout << pb1->_b << endl;
//}

void fun(A* pa)
{
	// dynamic_cast会先检查是否能转换成功(指向子类对象),能成功则转换,
	// (指向父类对象)不能则返回NULL
	B* pb1 = dynamic_cast<B*>(pa);
	if (pb1)
	{
		cout << "pb1:" << pb1 << endl;
		cout << pb1->_a << endl;
		cout << pb1->_b << endl;
		pb1->_a++;
		pb1->_b++;
		cout << pb1->_a << endl;
		cout << pb1->_b << endl;
	}
	else
	{
		cout << "转换失败" << endl;
	}
}

int main()
{
	A a;
	B b;
	fun(&a);
	fun(&b);

	return 0;
}

3.RTTI

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值