ex_this.cpp

来自「Visual C++应用教程-源代码 本书在复习C++基础知识后」· C++ 代码 · 共 35 行

CPP
35
字号
// [例Ex_This] 使用this指针
#include <iostream.h>

class CPoint
{
public:
	CPoint( int x = 0, int y = 0 ) 	
	{ 	
		this->xPos = x; 		this->yPos = y; 	
	} 
public:	// 加操作
	void Copy( CPoint one)
	{
		*this = one;			// 直接通过this赋值
	}

public:
	void  Print() 	
	{ 
		cout<<"Point("<<xPos<<", " <<yPos<<")"<< endl; 	
	}  
private:    
	int xPos,  yPos;
};

int main()
{
	CPoint pt1(10, 20), pt2(30, 40);
	pt1.Print();
	pt1.Copy(pt2);
	pt1.Print();
	return 0;
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?