天天看點

VS2010 C++ 學習筆記(六) this指針 const 指針 引用

this指針的用法 慕課網的 連結 http://www.imooc.com/video/8153

普通例子,不用this指針。

Array.h

class Array
{
public:
	Array(int len);
	~Array();
	void setLen(int len);
	int getLen();
	void printInfo();
private:
	int m_iLen;
};
           

Array.cpp

#include "Array.h"
#include <iostream>

using namespace std;

Array::Array(int len)
{
	m_iLen = len;
}
Array::~Array()
{

}
void Array::setLen(int len)
{
	m_iLen = len;
}
int Array::getLen()
{
	return m_iLen;
}

void Array::printInfo()
{
	cout << "len " << m_iLen << endl;
}
           

demo.cpp

#include <iostream>
#include <stdlib.h>
#include "Array.h"

using namespace std;

/************************************************************************/
/*	定義一個Array類
	資料成員:m_iLen表示數組長度
	成員函數:
		構造函數
		析構函數
		len的封裝函數
		資訊的輸出函數printInfo
*/
/************************************************************************/

int main(void)
{

	Array arr1(10);

	system("pause");
	return 0;
}
           

---------------------------------------------------------------------------------------------------------------------------

this 指針例子

Array.h

class Array
{
public:
	Array(int len);
	~Array();
	void setLen(int len);
	int getLen();
	void printInfo();
private:
	int len;
};
           

Array.cpp

#include "Array.h"
#include <iostream>

using namespace std;

Array::Array(int len)
{
	this->len = len;
}
Array::~Array()
{

}
void Array::setLen(int len)
{
	this->len = len;
}
int Array::getLen()
{
	return this->len;
}

void Array::printInfo()
{
	//cout << "len " << m_iLen << endl;
}

           

demo.cpp

#include <iostream>
#include <stdlib.h>
#include "Array.h"

using namespace std;

/************************************************************************/
/*	定義一個Array類
	資料成員:m_iLen表示數組長度
	成員函數:
		構造函數
		析構函數
		len的封裝函數
		資訊的輸出函數printInfo
*/
/************************************************************************/

int main(void)
{

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


/************************************************************************/
//  10
//  請按任意鍵繼續. . .                                                                    
/************************************************************************/
           

**********************************this 指針 *****************************************

#include <iostream>
#include <stdlib.h>
#include "Array.h"

using namespace std;

/************************************************************************/
/*	定義一個Array類
	資料成員:m_iLen表示數組長度
	成員函數:
		構造函數
		析構函數
		len的封裝函數
		資訊的輸出函數printInfo
*/
/************************************************************************/

int main(void)
{

	Array arr1(10);
	arr1.printInfo().setLen(5); //setLen(5) 并非改變arr1裡面的len的值。
	//傳回出去Array Array::printInfo()的Array是一個臨時對象。是以無法改變arr1的值。
	//若想改變讓Array是arr1對象的引用就可以達到該目的。
	//在Array.cpp 裡面修改為 Array& Array::printInfo()
	//在Array.h 裡面修改為 Array& printInfo();即可實作該目的。
	cout << "len= " << arr1.getLen()<< endl;
	system("pause");
	return 0;
}

           

Array.h

class Array
{
public:
	Array(int len);
	~Array();
	void setLen(int len);
	int getLen();
	Array& printInfo();
private:
	int len;
};
           

Array.cpp

#include "Array.h"
#include <iostream>

using namespace std;

Array::Array(int len)
{
	this->len = len;
}
Array::~Array()
{

}
void Array::setLen(int len)
{
	this->len = len;
}
int Array::getLen()
{
	return this->len;
}

Array& Array::printInfo()
{
	cout << "len= " << len << endl;
	return *this; 
	//this 本身是個指向對象的指針,加*,則是個對象。傳回類型為對象。
}

           
VS2010 C++ 學習筆記(六) this指針 const 指針 引用

****************************************

VS2010 C++ 學習筆記(六) this指針 const 指針 引用

****************************************************

VS2010 C++ 學習筆記(六) this指針 const 指針 引用
#include <iostream>
#include <stdlib.h>
using namespace std;
class Coordinate
{

public:
	Coordinate(int x, int y)
	{
		// 設定X,Y的坐标
		m_iX = x;
		m_iY = y;
	}
	// 實作常成員函數
	void printInfo() const
	{
		cout << "("<< m_iX <<","<< m_iY <<")"<<endl;
	}
public:
	int m_iX;
	int m_iY;
};


int main(void)
{
	const Coordinate coor(3, 5);

	// 建立常指針p
	const Coordinate *p = &coor;
	// 建立常引用c
	const Coordinate &c = coor;

	coor.printInfo();
	p->printInfo();
	c.printInfo();  

	system("pause");

	return 0;
}
           

void printInfo() const

改為 

void printInfo() 會出現如下error

1>h:\users\lin\documents\visual studio 2010\projects\mooc\mooc\demo.cpp(33): error C2662: “Coordinate::printInfo”: 不能将“this”指針從“const Coordinate”轉換為“Coordinate &”

1>          轉換丢失限定符

1>h:\users\lin\documents\visual studio 2010\projects\mooc\mooc\demo.cpp(34): error C2662: “Coordinate::printInfo”: 不能将“this”指針從“const Coordinate”轉換為“Coordinate &”

1>          轉換丢失限定符

1>h:\users\lin\documents\visual studio 2010\projects\mooc\mooc\demo.cpp(35): error C2662: “Coordinate::printInfo”: 不能将“this”指針從“const Coordinate”轉換為“Coordinate &”

1>          轉換丢失限定符

1>

1>生成失敗。