//this指針和const
#if 1
class A
{
public:
//普通成員函數
A(int a, int b)// ====》A( A *pThis, int a, int b);
{
this->a = a;
this->b = b;
}
void printA()//==>void printA(A * this)
{
cout << "a = " << a << endl;
cout << "b = " << this->b << endl;
}
void opVar1(int a, int b)//==> void opVar1(A * const pThis, int a, int b)
{
a = 100;
//若類成員函數的形參 和 類的屬性,名字相同,通過this指針來解決。
this->a = a;
}
/*
void const opVar2(int a, int b);
void opVar2(int a, int b) const;
1、const寫在什麼位置 沒有關係
2、const修飾的是this指針所指向的內存空間,修飾的是this指針.
3、指針變量與指針所指向的內存空間 是兩個不同的概念
*/
//類的成員函數可通過const修飾,請問const修飾的是誰?
//void const opVar2(int a, int b)
void opVar2(int a, int b) const //==>void opVar1(const A * const pThis,int a, int b)
{
a = 100;
//this->a = 80;//err
}
protected:
private:
int a;
int b;
};
void test()
{
A a1(1, 2);
a1.printA();//==> printA(&a1);
}
#endif
028_this指針和成員函數中加入const後修飾的是誰
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.