C++ 类型自动转换 构造函数 复制构造函数 赋值操作运算符函数

什么都不说,直接上代码。

#pragma once
#include <iostream>

class Core
{
public:
	Core(){ std::cout << __FUNCSIG__ << ":" << this<<std::endl; }
	Core(const Core&){ std::cout << __FUNCSIG__ << ":" << this << std::endl; }
	~Core(){ std::cout << __FUNCSIG__ << ":" << this << std::endl; }

	Core* clone()const{ return new Core(*this); }
};


#pragma once
#include <iostream>

template <class T>
class Handle
{
public:
	Handle() :ptr(NULL){ std::cout << __FUNCSIG__ << ":" << this << std::endl; }
	Handle(T* t) :ptr(t){ std::cout << __FUNCSIG__ << ":" << this << std::endl; }
	~Handle(){ std::cout << __FUNCSIG__ << ":" << this << std::endl; delete ptr; }
	Handle(const Handle& ref){
		std::cout << __FUNCSIG__ << ":" << this << std::endl;
		ptr = ref.ptr ? ref.ptr->clone() : NULL;
	}
	Handle& operator=(const Handle& ref){
		std::cout << __FUNCSIG__ << ":" << this << std::endl;
		if (&ref != this)
		{
			delete ptr;
			ptr = ref.ptr ? ref.ptr->clone() : NULL;
		}
		return *this;
	}
private:
	T* ptr;
};

#include <iostream>
#include "Core.h"
#include "Handle.h"

int main(int __argc, const char** __argv)
{
	Handle<Core> h;
	h = new Core();
	std::cout << "-----------------" << std::endl;
	Handle<Core> handle = new Core();
	std::cout << "-----------------" << std::endl;
	return EXIT_SUCCESS;
}
执行结果:
__thiscall Handle<class Core>::Handle(void):0023F8DC
__thiscall Core::Core(void):00541508
__thiscall Handle<class Core>::Handle(class Core *):0023F7C8
class Handle<class Core> &__thiscall Handle<class Core>::operator =(const class
Handle<class Core> &):0023F8DC
__thiscall Core::Core(const class Core &):00541538
__thiscall Handle<class Core>::~Handle(void):0023F7C8
__thiscall Core::~Core(void):00541508
-----------------
__thiscall Core::Core(void):00541508
__thiscall Handle<class Core>::Handle(class Core *):0023F8D0
-----------------
__thiscall Handle<class Core>::~Handle(void):0023F8D0
__thiscall Core::~Core(void):00541508
__thiscall Handle<class Core>::~Handle(void):0023F8DC
__thiscall Core::~Core(void):00541538
请按任意键继续. . .



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值