实验1:若类成员函数的形参 和 类的属性,名字相同,通过this指针来解决。
实验2:类的成员函数可通过const修饰,请问const修饰的是谁
const修饰的是属性 this->a this->b都会报错,修饰的是this指针所指向的内存空间
#include <iostream>
using namespace std;
class Test
{
public:
Test(int a, int b) //---> Test(Test *this, int a, int b)
{
this->a = a;
this-> b = b;
}
void printT()
{
cout<<"a: " <<a <<endl;
cout<< "b: " << this->b <<endl;
}
//void OpVar(int a,int b) const //const的位置没有关系
//const修饰的是
//const修饰的形参a? 不是
//const修饰的是属性 this->a this->b都会报错,修饰的是this指针所指向的内存空间
void const OpVar(int a,int b) //==》 void const OpVar(const Test *this,int a,int b)
{
cout<<"a: " <<a <<endl;
cout<< "b: " << this->b <<endl;
}
protected:
private:
int a;
int b;
};
void main()
{
Test t1(1, 2);
t1.printT();// ===> printT(&t1)
cout<<"hello..."<<endl;
system("pause");
return ;
}