文章目录
- 1.只能在堆上创建对象的类
 - 1.1析构函数私有化
 - 1.2析构函数 = delete
 - 1.3构造函数私有定义+拷贝构造私有只声明
 - 1.4构造函数私有定义+拷贝构造 = delete
 
- 2.不能被拷贝的类
 - 2.1 私有声明不定义拷贝构造函数
 - 2. 2拷贝构造函数 = delete
 
- 3.只能在栈和静态区创建对象的类
 - 4.不能被继承的类
 - 4.1final关键字
 - 4.2基类构造函数私有化
 
1.只能在堆上创建对象的类
1.1析构函数私有化
class HeapOnly
{
public:
	void Destroy()
	{
		delete this;
	}
	/*void Destroy()
	{
		delete[] _ptr;
		operator delete(this);
	}*/
private:
	~HeapOnly()
	{
		cout << "~HeapOnly()" << endl;
	}
	int* _ptr;
};
int main()
{
	//HeapOnly ho1;
	//static HeapOnly ho2;
	HeapOnly* pho3 = new HeapOnly;
	pho3->Destroy();
	return 0;
}
 
1.2析构函数 = delete
class HeapOnly
{
public:
	HeapOnly()
	{
		_str = new char[10];
	}
	~HeapOnly() = delete;
	void Destroy()
	{
		delete[] _str;
		operator delete(this);
	}
private:
	char* _str;
};
int main()
{
	//堆上创建对象
	HeapOnly* ptr = new HeapOnly;
    ptr->Destroy();
    
	//栈上创建对象
	//HeapOnly hp1;
	//数据段上[静态区]创建对象
	//static HeapOnly hp2;
	return 0;
}
 
1.3构造函数私有定义+拷贝构造私有只声明
//构造函数私有化
class HeapOnly
{
public:
	//设置成静态成员函数的目的 在类外不需生成对象就可调用
	static HeapOnly* CreateObj(int x = 0)
	{
		HeapOnly* p = new HeapOnly(x);
		return p;
	}
private:
	//私有 只声明不实现
	//私有: 类外无法访问创建对象 
	//只声明: 压根就没想让别人用 声明毫无意义
	//不实现: 防止实现了在类内函数拷贝创建
	HeapOnly(int x = 0)
		:_x(x)
	{
	}
	HeapOnly(const HeapOnly& hp);
	HeapOnly& operator=(const HeapOnly& hp);
	int _x = 0;
};
int main()
{
	//HeapOnly ho1;
	//static HeapOnly ho2;
	//HeapOnly* pho3 = new HeapOnly;
	//静态成员函数才能这样访问
	HeapOnly* p1 = HeapOnly::CreateObj(1);
	
	//为防止拷贝构造在栈上创建对象 需要处理拷贝构造
	//HeapOnly p2(*p1);
	return 0;
}
 
1.4构造函数私有定义+拷贝构造 = delete
//构造函数私有化
class HeapOnly
{
public:
	//设置成静态成员函数的目的 在类外不需生成对象就可调用
	static HeapOnly* CreateObj(int x = 0)
	{
		HeapOnly* p = new HeapOnly(x);
		return p;
	}
	HeapOnly(const HeapOnly& hp) = delete;
	HeapOnly& operator=(const HeapOnly& hp) = delete;
private:
	//私有 只声明不实现
	//私有: 类外无法访问创建对象 
	//只声明: 压根就没想让别人用 声明毫无意义
	//不实现: 防止实现了在类内函数拷贝创建
	HeapOnly(int x = 0)
		:_x(x)
	{
	}
	int _x = 0;
};
int main()
{
	//HeapOnly ho1;
	//static HeapOnly ho2;
	//HeapOnly* pho3 = new HeapOnly;
	//静态成员函数才能这样访问
	HeapOnly* p1 = HeapOnly::CreateObj(1);
	
	//为防止拷贝构造在栈上创建对象 需要处理拷贝构造
	//HeapOnly p2(*p1);
	return 0;
}
 
2.不能被拷贝的类
上述已经讲述
2.1 私有声明不定义拷贝构造函数
2. 2拷贝构造函数 = delete
3.只能在栈和静态区创建对象的类
class StackOnly
{
public:
	static StackOnly CreateObj(int x = 0)
	{
		return StackOnly(x);
	}
private:
	StackOnly(int x = 0)
		:_x(x)
	{
	}
	int _x;
};
int main()
{
	/*
	StackOnly st1;
	static StackOnly st2;
	StackOnly* st3 = new StackOnly;
	*/
	StackOnly st1 = StackOnly::CreateObj(1);
	static StackOnly st2 = st1;
	return 0;
}
 
能不能只在栈上创建??? 禁用拷贝构造 启用移动构造
class StackOnly
{
public:
//不能传引用返回   因为StackOnly(x)是个局部对象
	static StackOnly CreateObj(int x = 0)
	{
		return StackOnly(x);
	}
	StackOnly(StackOnly&& st)
		:_x(st._x)
	{
	
	}
private:
	StackOnly(int x = 0)
		:_x(x)
	{
	
	}
	StackOnly(const StackOnly& st) = delete;
	int _x;
};
int main()
{
	/*
	StackOnly st1;
	static StackOnly st2;
	StackOnly* st3 = new StackOnly;
	*/
	StackOnly st1 = StackOnly::CreateObj(1);
	static StackOnly st2 = st1;
	//static StackOnly st2 = move(st1);
	return 0;
}
 

貌似可以 接着看

综上不可以!!!
4.不能被继承的类
4.1final关键字

4.2基类构造函数私有化





![2023年中国儿童滑板车优点、市场规模及发展前景分析[图]](https://img-blog.csdnimg.cn/img_convert/711022110611124cabb58f9a4af42996.png)














