【C++】特殊类设计&类型转换

💡前言

本篇文章的内容是C++的拓展学习,主要介绍在某些特定场合下的一些特殊类的设计,并且总结了C/C++中的类型转换

一,特殊类设计

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

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

(1) C++98的写法

将拷贝构造函数与赋值运算符重载只声明不定义,并且将其声明为私有即可

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

原因:

1.设置成私有:如果只声明没有设置成private,用户自己如果在类外定义了,就可以不能禁止拷贝了。

2.只声明不定义:不定义是因为该函数根本不会调用,定义了其实也没有什么意义,不写反而还简单,而且如果定义了就不会防止成员函数内部拷贝了。

(2) C++11写法

C++11扩展delete的用法,delete除了释放new申请的资源外,如果在默认成员函数后跟上=delete,表示让编译器删除掉该默认成员函数

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

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

实现方式1

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

class HeapOnly
{
public:
	// 只打开唯一的通道在堆上new
	static HeapOnly* CreateObj()
	{
		return new HeapOnly;
	}

	HeapOnly(const HeapOnly& hp) = delete;
	HeapOnly operator=(const HeapOnly& hp) = delete;
private:
	// 构造函数私有化
	HeapOnly()
	{}
};

int main()
{
	HeapOnly* hp = HeapOnly::CreateObj();
	delete hp;

	return 0;
}

实现方式2

(1) 将类的析构函数私有,让其他实例化出的对象无法销毁
(2) 直接在堆上new对象,再提供一个公有的delete函数

class HeapOnly
{
public:
	void  Destory()
	{
		delete this;
	}

private:
	// 析构函数私有化
	~HeapOnly()
	{}
};

int main()
{
	HeapOnly* hp = new HeapOnly;
	hp->Destory();

	return 0;
}

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

实现方式

同上将构造函数私有化,然后设计静态方法创建对象返回即可

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()
{
	StackOnly s4 = StackOnly::CreateObj();

	return 0;
}

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

(1) C++98方式

C++98中构造函数私有化,派生类中调不到基类的构造函数。则无法继承

class NonInherit
{
public:
 static NonInherit GetInstance()
 {
 return NonInherit();
 }
private:
 NonInherit()
 {}
};

(2) C++11方法

final关键字,final修饰类,表示该类不能被继承

class A  final
{
    // ....
};

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

单例:全局只有唯一实例化对象。

5.1 饿汉模式

饿汉模式就是进入min函数之前创建好对象,需要时直接调用公有函数获取

class InforMgr
{
public:
	// 获取对象
	static InforMgr& GetInstance()
	{
		return _ins;
	}
	
	// 这个函数只是用来说明问题
	void Print()
	{
		cout << _ip << endl;
		cout << _port << endl;
	}

private:
	InforMgr(const InforMgr& ins) = delete;
	InforMgr& operator=(const InforMgr& ins) = delete;

	InforMgr()
	{
		cout << "InforMgr()" << endl;
	}
private:
	string _ip = "100.02.4";
	size_t _port = 3;

	//提前创建一个静态的全局对象
	static InforMgr _ins;
};

//在类外定义
InforMgr InforMgr::_ins;

int main()
{
	InforMgr::GetInstance().Print();

	return 0;
}

饿汉模式的问题

(1) 有多个饿汉模式的单例存在,某个对象初始化内容较多(读文件),会导致程序启动慢
(2) A和B两个饿汉,对象初始化存在依赖关系,要求A先初始化,B在初始化,饿汉无法保证

5.2 懒汉模式

懒汉模式就是:不是提前创建好对象,而是什么时候需要,什么时候创建

(1) C++98方式

class InforMgr
{
public:
	// 获取对象
	static InforMgr& GetInstance()
	{
		// 第一次调用才new一个单例对象出来
		if (_pins == nullptr)
			_pins = new InforMgr;
		
		// 后面再调用时就直接返回这个对象
		return *_pins;
	}

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

private:
	InforMgr(const InforMgr& ins) = delete;
	InforMgr& operator=(const InforMgr& ins) = delete;

	InforMgr()
	{
		cout << "InforMgr()" << endl;
	}
private:
	string _ip = "100.02.4";
	size_t _port = 3;

	static InforMgr* _pins;
};

// 在类外定义
InforMgr* InforMgr::_pins = nullptr;

int main()
{
	InforMgr::GetInstance().Print();

	return 0;
}

(2) C++11方式

class InforMgr
{
public:
	// 获取对象
	static InforMgr& GetInstance()
	{
		// 局部静态,第一次调用时才创建单例对象
		// C++11之后
		static InforMgr ins;
		return ins;
	}

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

private:
	InforMgr(const InforMgr& ins) = delete;
	InforMgr& operator=(const InforMgr& ins) = delete;

	InforMgr()
	{
		cout << "InforMgr()" << endl;
	}
private:
	string _ip = "100.02.4";
	size_t _port = 3;

	static InforMgr* _pins;
};

// 在类外定义
InforMgr* InforMgr::_pins = nullptr;

int main()
{
	InforMgr::GetInstance().Print();

	return 0;
}

懒汉模式的问题

线程安全的风险

二,类型转换

1. 内置类型之间

隐式类型转化:编译器在编译阶段自动进行,能转就转,不能转就编译失败。显式类型转化:需要用户自己处理

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

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

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

	return 0;
}

2. 内置类型和自定义类型之间

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; // ok
	A aa2 = { 2,2 }; // 多参数隐式类型转换
	const A& aa3 = { 2,2 };
	
	// 自定义->内置
	int x = (int)aa1;
	//本质是:
	//int z = aa1.operator int();

	int x = aa1;
	int y = aa2;
	cout << x << endl;
	cout << y << endl;

	return 0;
}

3. 自定义类型和自定义类型之间

通过对应法构造函数实现

class A
{
public:
	//explicit A(int a)
	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)
	{}
	
	// 把A转换成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;
}

三,C++强制类型转换

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

1. static_cast

static_cast 对应隐式类型转换 – 数据的意义没有改变

int main()
{
  double d = 12.34;
  
  int a = static_cast<int>(d);
  cout<<a<<endl;
  
  return 0;
}

2. reinterpret_cast

reinterpret_cast 对应强制类型转换 – 数据的意义已经发生改变

int main()
{
  double d = 12.34;
  
  int a = static_cast<int>(d);
  cout << a << endl; // 12

  // 对应强制类型转换--数据的意义已经发生改变
  int* p1 = reinterpret_cast<int*>(a);

  return 0;
}

3. const_cast

const_cast 对应强制类型转换中有风险的去掉const属性

void Test ()
{
  const int a = 2;
  int* p = const_cast< int*>(&a);
  *p = 3;
  
  cout<< a <<endl; // 2
}

4. dynamic_cast

dynamic_cast用于将一个父类对象的指针/引用转换为子类对象的指针或引用(动态转换)

向上转型:子类对象指针/引用->父类指针/引用(不需要转换,赋值兼容规则)

向下转型:父类对象指针/引用->子类指针/引用(用dynamic_cast转型是安全的)

注意

(1) dynamic_cast只能用于父类含有虚函数的类
(2) dynamic_cast会先检查是否能转换成功,能成功则转换,不能则返回0

1.没有用 dynamic_cast 时:

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

	int _a = 1;
};

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

void fun(A* pa)
{
	// 指向父类转换时是有风险的,后续访问存在越界访问的风险
	// 指向子类转换时安全
	
	B* pb1 = (B*)pa;
	cout << "pb1:" << pb1 <<endl;
	cout << "pb1->_a = " << pb1->_a << endl;
	cout << "pb1->_b = " << pb1->_b << endl; //指向父类时会越界
	
	// 下面的代码直接会报错
	//pb1->_a++;
	//pb1->_b++;
	//cout << pb1->_a << endl;
	//cout << pb1->_b << endl;
}

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

	return 0;
}

在这里插入图片描述

2.使用 dynamic_cast 时:

dynamic_cast 会先检查是否能转换成功,若指向子类对象,就能成功则转换,若指向父类对象,不能转换成功,则返回NULL

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

	int _a = 1;
};

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

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

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

	return 0;
}

在这里插入图片描述

  • 11
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值