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

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

#pragma once
#include 

class Core
{
public:
	Core(){ std::cout << __FUNCSIG__ << ":" << this<


#pragma once
#include 

template 
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 
#include "Core.h"
#include "Handle.h"

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



你可能感兴趣的:(程序设计语言,C++,类型自动转换,构造函数,复制构造函数,赋值操作运算符函数)