0
点赞
收藏
分享

微信扫一扫

空指针的简单操作

敬亭阁主 2022-04-30 阅读 35
#include "iostream"
 
using namespace std;
 
//空指针调用成员函数
 
class Person
{
	public:
 
				void  showPerson()
				{
					cout << "this is person" << endl;
				};
				void  showPersoAge()
				{
					if(this == NULL)
					{
						return;  //避免传入空指针导致引用异常
					}
 
					cout << "age ==" << this->m_Age <<endl;
				};
	int m_Age;
};
 
void test01()
{
	Person* p = NULL;
	p->showPerson();  //空指针可访问成函数----不操作成员变量
	p->showPersoAge();  //空指针不可访问成员变量
 
}
int main(void)
{
test01();
system("pause");
}
举报

相关推荐

0 条评论