class AAA
{
string m_str;
public:
AAA(){ };
~AAA(){};
void print1() //在该程序中 对象a为空,但该函数可以正常输出,这里进行的只是调用该函数地址是固定的,和a没有关系。
{
cout << "Hello CSDN!" <<endl;
}
void print2() //在该函数中,当调用成员变量m_str时会调用this指针,但此时this是a指向的地址,为空则调用空指针的对象,程序崩溃。
{
m_str = "abc";
}
virtual void print3() //该函数是虚函数,C++调用虚函数的时候,要根据实例中虚函数表指针(this)得到虚函数表,再从虚函数表中找到函数的地址。
{
cout << "I'm wrong!!!" <<endl;
}
};
int _tmain(int argc, _TCHAR* argv[])
{
AAA* a = NULL;
a->print1();
system("PAUSE");
a->print2();
system("pause");
a->print3();
system("pause");
return 0;
}
类中的一些指针调用
最新推荐文章于 2022-11-19 19:45:47 发布