理解运算符重载 5
#include "stdafx.h"
#include <windows.h>
#include <iostream>
#include <string>
/*
重载成员访问操作符 ->, *,主要用于支持指针型类,如迭代器。一般箭头操作符必须定义为类的成员函数,解引用*一般也
定义为类的成员
*/
namespace operator_test
{
class CObject
{
public:
CObject(std::string name, int age) :m_name(name), m_age(age)
{
}
std::string& GetName()
{
return m_name;
}
int GetAge()
{
return m_age;
}
private:
std::string m_name;
int m_age;
};
class CPtrMgr
{
public:
CPtrMgr() : m_ptr(NULL)
{
}
CPtrMgr(CPtrMgr& ptr) //拷贝构造函数
{
m_ptr = ptr.GetPtr();
}
CPtrMgr(void* Ptr)
{
std::cout << "init ptr: " << Ptr << std::endl;
m_ptr = Ptr;
}
~CPtrMgr()
{
std::cout << "delete ptr: " << m_ptr << std::endl;
delete m_ptr;
m_ptr = NULL;
}
void* GetPtr()
{
return m_ptr;
}
void SetPtr(void* Ptr)
{
m_ptr = Ptr;
}
private:
void* m_ptr;
};
}
void test_operator_test()
{
operator_test::CObject* Object = new operator_test::CObject("sysnap", 30);
std::cout << Object->GetName() << std::endl;
//
//万一忘了delete Object,就容易造成内存泄漏.
//
operator_test::CPtrMgr PtrMgr(Object);
//
//这样函数返回就可以删除指针了。
//
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include <windows.h>
#include <iostream>
#include <string>
/*
重载成员访问操作符 ->, *,主要用于支持指针型类,如迭代器。一般箭头操作符必须定义为类的成员函数,解引用*一般也
定义为类的成员
*/
namespace operator_test
{
class CObject
{
public:
CObject(std::string name, int age) :m_name(name), m_age(age)
{
}
std::string& GetName()
{
return m_name;
}
int GetAge()
{
return m_age;
}
private:
std::string m_name;
int m_age;
};
class CPtrMgr
{
public:
CPtrMgr() : m_ptr(NULL)
{
}
CPtrMgr(CPtrMgr& ptr) //拷贝构造函数
{
m_ptr = ptr.GetPtr();
}
CPtrMgr(CObject* Ptr)
{
std::cout << "init ptr: " << Ptr << std::endl;
m_ptr = Ptr;
}
~CPtrMgr()
{
std::cout << "delete ptr: " << m_ptr << std::endl;
delete m_ptr;
m_ptr = NULL;
}
CObject* operator->()
{
return m_ptr;
}
CObject& operator*()
{
return *m_ptr;
}
CObject* GetPtr()
{
return m_ptr;
}
void SetPtr(CObject* Ptr)
{
m_ptr = Ptr;
}
private:
CObject* m_ptr;
};
}
void test_operator_test()
{
operator_test::CObject* Object = new operator_test::CObject("sysnap", 30);
operator_test::CPtrMgr PtrMgr(Object);
//
//我们希望通过PtrMgr来访问Object里面的方法,就跟Object指针访问一样,需要重载 ->
//
std::cout << PtrMgr->GetName() << std::endl;
operator_test::CObject &realObject = *PtrMgr;
std::cout << realObject.GetName() << std::endl;
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include <windows.h>
#include <iostream>
#include <string>
/*
重载成员访问操作符 ->, *,主要用于支持指针型类,如迭代器。一般箭头操作符必须定义为类的成员函数,解引用*一般也
定义为类的成员
*/
namespace operator_test
{
class CObject
{
public:
CObject(std::string name, int age) :m_name(name), m_age(age)
{
}
std::string& GetName()
{
return m_name;
}
int GetAge()
{
return m_age;
}
private:
std::string m_name;
int m_age;
};
class CPtrMgr
{
public:
CPtrMgr() : m_ptr(NULL)
{
}
CPtrMgr(CPtrMgr& ptr) //拷贝构造函数
{
m_ptr = ptr.GetPtr();
ptr.SetPtr(NULL);
}
CPtrMgr& operator=(CPtrMgr& Ptr)
{
m_ptr = Ptr.GetPtr();
Ptr.SetPtr(NULL);
return *this;
}
CPtrMgr(CObject* Ptr)
{
std::cout << "init ptr: " << Ptr << std::endl;
m_ptr = Ptr;
}
~CPtrMgr()
{
std::cout << "delete ptr: " << m_ptr << std::endl;
delete m_ptr;
m_ptr = NULL;
}
CObject* operator->()
{
return m_ptr;
}
CObject& operator*()
{
return *m_ptr;
}
CObject* GetPtr()
{
return m_ptr;
}
void SetPtr(CObject* Ptr)
{
m_ptr = Ptr;
}
private:
CObject* m_ptr;
};
}
operator_test::CPtrMgr GetSafePtr()
{
operator_test::CObject* Object = new operator_test::CObject("sysnap", 30);
operator_test::CPtrMgr PtrMgr(Object);
return PtrMgr;
}
void test_operator_test()
{
operator_test::CPtrMgr PtrMgr = GetSafePtr(); //上面例子中,写operator_test::CPtrMgr GetSafePtr(),但这样会崩溃
//原因在于return PtrMgr;时已经把指针释放掉了。所以需要修改拷贝构造函数.释放控制权。
operator_test::CPtrMgr PtrMgr2 = PtrMgr;
std::cout << PtrMgr2->GetName() << std::endl;
operator_test::CObject &realObject = *PtrMgr2;
std::cout << realObject.GetName() << std::endl;
}