64,648
社区成员
发帖
与我相关
我的任务
分享
#include <iostream>
using namespace std;
class Student{
public:
Student(char *pname="no name"){
cout<<"create one student"<<endl;
name=new char(strlen(pname)+1);
strcpy(name,pname);
total++;
cout<<"学生人数: "<<total<<endl;
}
~Student(){
cout<<"destruct one student"<<endl;
total--;
cout<<"学生人数: "<<total<<endl;
delete[]name;//感觉这里有运行时有bug,注释改行bug消失
}
static int number(){
return total;
}
private:
char *name;
static int total;
};
int Student::total=0;
void fn(){
Student stu1;
Student stu2;
}
int main(void){
fn();
cout<<Student::number()<<endl;
//cout<<Student::total<<endl;
return 0;
}