using namespace std;
class Student
{
string name;
static int total;//用来统计学生总人数
public:
Student(string p="wang")
{
name=p;
total++;
};
static int GetTotal()
{
return total;
};
~Student(){total--;}
};
int Student::total=0;
int main()
{
cout<<"The number of all students: "<<0<<endl;
Student a;
cout<<"The number of all students: "<<a.GetTotal<<endl;
Student b("zhao");
cout<<"The number of all students: "<<b.GetTotal<<endl;
return 0;
}
using namespace std;
class Student
{
string name;
static int total;//用来统计学生总人数
public:
Student(string p="wang")
{
name=p;
total++;
};
static int GetTotal()
{
return total;
};
~Student(){total--;}
};
int Student::total=0;
int main()
{
cout<<"The number of all students: "<<0<<endl;
Student a;
cout<<"The number of all students: "<<a.GetTotal()<<endl;
Student b("zhao");
cout<<"The number of all students: "<<b.GetTotal()<<endl;
return 0;
}
cout<<a.GetTotal应该是返回了类里面GetTotal函数的地址吧,所以27,29行输出的是一样的
这不是一样的吗?