有三個文件:
student.h文件
#include <string>
#include <iostream>
using namespace std;
const int MAX_NAME_SIZE = 30;
class Student{
public:
Student(const char * pszName);
~Student();
public:
static void PrintfAllStudents();
private:
char m_name[MAX_NAME_SIZE];
Student *next;
Student *prev;
static Student *m_head; //這裏爲什麼後面沒看到它的初始化語句??
};
function.cc文件
#include <iostream>
#include <string>
#include <string.h>
#include "student.h"
using namespace std;
Student::Student(const char *pszName)
{
strcpy(this->m_name,pszName);
this -> next = m_head;
this -> prev =NULL;
if(m_head != NULL)
m_head -> prev = this;
m_head = this;
}
Student::~Student(){
if(this == m_head)
{
m_head = this-> next;
}
else{
this -> prev->next = this->next;
this -> next -> prev = this -> prev;
}
}
void Student::PrintfAllStudents()
{
for(Student *p =m_head;p!= NULL;p=p->next)
printf("%s\n",p->m_name);
}
Student *Student::m_head = NULL;
main.cc文件
#include <iostream>
#include <string>
#include "student.h"
using namespace std;
int main()
{
Student studentA("AAA");
Student studentB("BBB");
Student studentC("CCC");
Student studentD("DDD");
Student student("MoreWindows");
cout << "begin to print : " << endl;
Student::PrintfAllStudents();
return 0;
}
爲什麼沒有看到 Student的靜態類型成員Student *m_head的初始化語句?但是依然便以正確