this指针定义概念
#include <iostream>
using namespace std;
class Person
{
public :
Person(int age)
{
this->age = age;
}
Person & PersonAddAge(Person &p)
{
this->age += p.age;
return *this;
}
int age;
};
void test01()
{
Person p1(18);
cout << "p1的年龄为:" << p1.age << endl;
}
void test02()
{
Person p1(10);
Person p2(10);
p2.PersonAddAge(p1).PersonAddAge(p1).PersonAddAge(p1).PersonAddAge(p1).PersonAddAge(p1);
cout << "p2的年龄为:" << p2.age << endl;
}
int main()
{
test01();
test02();
cout << "\n " << endl;
system("pause");
return 0;}
空指针访问成员函数
#include <iostream>
using namespace std;
class Person
{
public:
void showClassName()
{
cout << "this is Person class" << endl;
}
void showPersonAge()
{
if (this == NULL)
{
return;
}
cout << "age=" <<this -> m_age << endl;
}
int m_age;
};
void test01()
{
Person* p = NULL;
p->showClassName();
}
int main()
{
test01();
cout << "\n " << endl;
system("pause");
return 0;
}
const修饰的成员函数
#include <iostream>
using namespace std;
class Person
{
public :
void showPerson()const
{
this->m_B = 100;
}
void func()
{
}
int m_A;
mutable int m_B;
};
void test01()
{
Person p;
p.showPerson();
}
void test02()
{
const Person p;
p.m_B = 100;
p.showPerson();
}
int main()
{
test01();
test02();
cout << "\n " << endl;
system("pause");
return 0;
}