今天這篇博文主要講解在C++中關鍵字struct和class的區別。我的這篇博文,就按照系統的將這兩個關鍵字的不同面進行詳細的講解。www.2cto.com 從語法上來講,class和struct做類型定義時只有兩點區別: (1)默認繼承權限,如果不指定,來自class的繼承按照private繼承處理,來自struct的繼承按照public繼承處理; (2)成員的默認訪問權限。class的成員默認是private權限,struct默認是public權限。以上兩點也是struct和class最基本的差別,也是最本質的差別; 但是在C++中,struct進行了擴展,現在它已經不僅僅是一個包含不同數據類型的數據結構 了,它包括了更多的功能; 首先,struct能包含成員函數嗎?是的,答案是肯定的。 好的,寫一段代碼驗證一下: [cpp] #include "stdafx.h" struct Test { int a; int getA() { return a; } void setA(int temp) { a = temp; } }; int _tmain(int argc, _TCHAR* argv[]) { Test testStruct; testStruct.setA(10); cout<<"Get the value from struct:"<<testStruct.getA()<<endl; Test *testStructPointer = new Test; testStructPointer->setA(20); cout<<"Get the value from struct again:"<<testStructPointer->getA()<<endl; delete testStructPointer; return 0; } 以上的代碼會很正確的運行,是的;沒錯,struct能包含成員函數的。 第二,struct有自己的構造函數嗎?是的,可以的。 #include "stdafx.h" struct Test { int a; Test() { a = 100; } int getA() { return a; } void setA(int temp) { a = temp; } }; int _tmain(int argc, _TCHAR* argv[]) { Test testStruct; testStruct.setA(10); cout<<"Get the value from struct:"<<testStruct.getA()<<endl; Test *testStructPointer = new Test; testStructPointer->setA(20); cout<<"Get the value from struct again:"<getA()<<endl; delete testStructPointer; // test the constructor Test testConstructor; cout<<"Set the value by the construct and get it:"<<testConstructor.getA()<<endl; return 0; } 第三,struct可以有析構函數麼?讓我來驗證一下; [cpp] #include "stdafx.h" struct Test { int a; Test() { a = 100; } int getA() { return a; } void setA(int temp) { a = temp; } ~Test() { cout<<"Destructor function called."<<endl; } }; int _tmain(int argc, _TCHAR* argv[]) { Test testStruct; testStruct.setA(10); cout<<"Get the value from struct:"<<testStruct.getA()<<endl; Test *testStructPointer = new Test; testStructPointer->setA(20); cout<<"Get the value from struct again:"<getA()<<endl; delete testStructPointer; // test the constructor Test testConstructor; cout<<"Set the value by the construct and get it:"<<testConstructor.getA()<<endl; return 0; } 是的,完全支持析構函數。 第四,struct支持繼承麼?再讓我寫代碼驗證一下; [cpp] #include "stdafx.h" struct A { int a; A() { a = 10; } void print() { cout<<"I am from A"<<endl; } }; struct B : A { int b; B() { a = 30; // set a to 30 b = 20; } /*void print() { cout<<"I am from B"<<endl; }*/ }; int _tmain(int argc, _TCHAR* argv[]) { B b1; cout<<b1.a<<endl; cout<<b1.b<<endl; b1.print(); A a1; cout<<a1.a<<endl; a1.print(); return 0; } 運行上述代碼,struct支持繼承。 第五,struct支持多肽麼?寫代碼測試一下便知; [cpp] #include "stdafx.h" struct A { virtual void print() = 0; }; struct B : A { void print() { cout<<"I am from B"<<endl; } }; struct C : A { void print() { cout<<"I am from C"<<endl; } }; int _tmain(int argc, _TCHAR* argv[]) { A *a1; B *b1 = new B; C *c1 = new C; a1 = b1; a1->print(); // call B, not A a1 = c1; a1->print(); // call C, not A return 0; } 第六,struct支持private、protected和public關鍵字麼? [cpp] #include "stdafx.h" struct A { private: int b; protected: int c; public: A() { b = 10; c = 20; d = 30; } int d; }; struct B : A { void printA_C() { cout<<A::c<<endl; }; // private member can not see /*void printA_B() { cout<<A::b<<endl; }*/ void printA_D() { cout<<A::d<<endl; } }; int _tmain(int argc, _TCHAR* argv[]) { A a1; B b1; // private member can not see //cout<<a1.b<<endl; // protected member can not see //cout<<a1.c<<endl; // public member can see cout<<a1.d<<endl; return 0; } 寫了這麼多了,那麼會出現這種一個狀況,如果是class的父類是struct關鍵字描述的,那麼默認訪問屬性是什麼? 當出現這種情況時,到底默認是public繼承還是private繼承,取決於子類而不是基類。class可以繼承自struct修飾的類;同時,struct也可以繼承自class修飾的類,繼承屬性如下列描述: [cpp] struct A{}; class B:A{}; // private 繼承 class A{}; struct B:A{}; // public 繼承 最後,那麼到底是使用struct,還是使用class呢?這個看個人喜好,但是這裡有一個編程規范的問題,當你覺得你要做的更像是一種數據結構的話,那麼用struct,如果你要做的更像是一種對象的話,那麼用class。