如果类没有被创建,则没有对应的成员,也就无法访问
下面这种方法需要先创建类,并将类作为参数传递给静态成员函数才能访问。
-
#include <iostream>
-
using namespace std;
-
-
-
class person {
-
public:
-
virtual void get_name()=0;
-
};
-
-
-
class student:public person
-
{
-
public:
-
void get_name()
-
{
-
cout <<"student name"<<endl;
-
}
-
static void get_age(student *_s);
-
student();
-
private:
-
int m_age;
-
};
-
-
student::student()
-
{
-
m_age = 11;
-
}
-
-
void student::get_age(student *_s)
-
{
-
cout <<"student age: "<<_s->m_age<<endl;
-
}
-
-
student *create_person()
-
{
-
return new student();
-
}
-
-
int main()
-
{
-
student *p = create_person();
-
p->get_name();
-
student::get_age(p);
-
return 0;
-
}
阅读(670) | 评论(0) | 转发(0) |