类和对象-空指针访问函数

类和对象-空指针访问函数

#include<iostream>
using namespace std;
class person
{
    
    
public:
	void showpersonname()
	{
    
    
		cout << "是person类" << endl;
	}
	void showpersonage()
	{
    
    
		if (this == NULL)  //此条件语句起保护作用  
		{
    
    
			return;
		}
		cout << "age = " <<this->m_Age<< endl;
	}
	int m_Age;
};
void test01()
{
    
    
	person * p=NULL ;
	p->showpersonname();
	p->showpersonage();
}
int main()
{
    
    
	test01();
	system("pause");
	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_54673833/article/details/114003025