//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后修饰的是谁
最新推荐文章于 2024-05-31 16:20:34 发布