12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849 |
- //4.3.3 空指针访问成员函数
- //C++中空指针也是可以调用成员函数的,但是也要注意有没有用到this指针
- //如果用到this指针,需要加以判断保证代码的健壮性
- #if(0)
- #include <iostream>
- class Person
- {
- public:
- void showClassName()
- {
- std::cout << "This is Person class" << std::endl;
- }
- void showPersonAge()
- {
- //报错的原因是因为传入的指针是NULL
- if (this == NULL)
- {
- return;
- }
- std::cout << "age = " << this->m_Age << std::endl;
- }
- int m_Age;
- };
- void test01()
- {
- Person* p = NULL;
-
- //p->showClassName();
-
- p->showPersonAge();
- }
- int main()
- {
- test01();
- system("pause");
- return 0;
- }
- #endif
|