C++ this指针练习

 

 

计算机无法判断是参数赋值给数据成员还是数据成员赋值给参数,所以我们需要一种this指针,this指针是指向对象数据成员的

 

 

using namespace std;
class Array
{

public:
	Array(int len)
	{
		cout << "调用了构造函数" << endl;
		this->len = len;

	}
	~Array() 
	{
		cout << "调用了析构函数" << endl;
	}
	void setLen(int len)
	{
		this->len = len;

	}
	int getLen()
	{
		return len;
	}
	void printinfo()
	{

	}
    
private:
	int len;       //<=>    this->len   

	
};

int main(void)
{
	Array arr1(10);
	cout << arr1.getLen() << endl;
		system("pause");
	return 0;
}

猜你喜欢

转载自blog.csdn.net/luoyir1997/article/details/83934012