现在的位置: 首页 > 综合 > 正文

赋值运算符重载

2012年05月11日 ⁄ 综合 ⁄ 共 580字 ⁄ 字号 评论关闭
// AssignmentOperatorTest.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <iostream>
#include <typeinfo>

using namespace std;

class point
{
public:
point(float x=0.0,float y=0.0)
{}
inline virtual point& operator =(const point &p);
protected:
float _x,_y;
};

inline point& point::operator =(const point &p)
{
cout<<"i am point"<<endl;
_x=p._x;//protected
_y=p._y;
return *this;
}

class point3d:public point
{
public:
inline virtual point3d& operator =(const point3d &p)
{
cout<<"i am point3d"<<endl;
return *this;
}
};



int _tmain(int argc, _TCHAR* argv[])
{
point3d pt3;

point &pp=pt3;

pp.operator =(pt3);

cout<<typeid(pp).name()<<endl;

getchar();

return 0;
}

抱歉!评论已关闭.