struct與class區別聯繫(轉)

傳送門:struct與class區別聯繫html

注意C中的struct和C++中的struct是不同的,c中的struct比較原生,僅僅是將一些屬性封裝起來構成一個總體,沒有OO的相關特性。而c++中的struct是對c中的struct進行擴展(兼容c中的struct),具有OO的特性,其實c++中的class能幹的事情struct幾乎都能幹,什麼繼承、多態等都OK。直接看下面代碼,不一樣編譯器對結果可能不同:c++

`#include <stdio.h>
struct A
{
int a;
//D:\github\cpp_hello_world>gcc -x c structtest.cpp
//structtest.cpp:7:5: error: expected ‘:’, ‘,’, ‘;’, ‘}’ or ‘__at
// _’ before ‘{’ token
void print() // pure c, this is not allowd
{
printf("a printf %d\n", a);
}
};git

struct B : A
{
int b;
B(int bb)
{
b = bb;
a = -1;
}
B(){b = 0; a = 2;}
};github

class C
{
public: // if there's no pubic, cannot use this way: C c = {11};
int c;
char d;
void func() //normal function(no construct/inherit .etc OO properties), you can also use this way: C c = {11};
{
printf("%d, %c\n", c, d);
}
//C(){}
};svn

struct D
{
int c;
char d;
D() // if there's no Construct or some other OO properties(like inherit), you can use this way: D d = {1,'y'}
{
c = -1;
d = 'x';
}
};函數

struct E
{
int c;
char d;
void func() //normal function, you can also use this way: E e = {2,'z'};
{
printf("%d, %c\n", c, d);
}
};優化

struct AA
{
private:
int a;
public:
int b;
};
class BClass: AA
{
public:
void fun()
{
printf("%d\n", b);
}
};
struct BStruct: AA
{
void func()
{
printf("%d\n", b);
}
};this

struct Base
{
virtual void fun()
{
printf("Base\n");
}
};
struct Child: Base
{
void fun()
{
printf("Child\n");
}
};debug

int main()
{
B b, b1(1);
printf("%d, %d \n", b.b, b.a);
printf("%d, %d \n", b1.b, b1.a);
A a = {10};
printf("%d\n", a.a);
C c = {11, 'a'};
C c1 = {'d'}; //convert to int
C c2 = {}; //init with default
C c3;
printf("%d, %c\n", c.c, c.d);
printf("%d, %c, %d\n", c1.c, c1.d, c1.d);
printf("%d, %d\n", c2.c, c2.d);
printf("%d, %c\n", c3.c, c3.d);//uninit, vs2012 will show Run-Time Check Failure #3 window,
c3.func();//uninit, but this way will pass the "Run-Time Check" in vs2012
//D d = {1,'y'}; //error: in C++98 ‘d’ must be initialized by constructo not by ‘{...}’
//printf("%d, %c\n", d.c, d.d);orm

E e = {2,'z'};
printf("%d, %c\n", e.c, e.d);
e.func();

BClass bclass;
BStruct bstruct;
bclass.fun();
bstruct.func();
//printf("%d", bclass.b); // 「AA::b」不可訪問,由於「BClass」使用「private」從「AA」繼承
printf("%d\n", bstruct.b); //OK

Base base;
Child child;
base.fun();
child.fun();
Base* base2 = &child;
base2->fun();

return 0;
}`

VisualStudio 2012默認debug和release結果:

Image(8)[4]73028934dad360862f6ed22a22a35c24

G++ 4.5.3, 默認和O2(g++ -O2 structtest.cpp)結果:

600b675239e56d6d70ac255f68e353f544e877ad37f3a2a6f5a77577dfb4adc5

mac下的g++(Apple LLVM version 5.1 (clang-503.0.40) (based on LLVM 3.4svn))無優化和O2結果
struct-and-class

不一樣編譯器結果不同主要是體如今printf函數實現(有空再研究下)下以及C c3的未初始化(注意c2和c3的區別)~ 這也告訴咱們必定要注意初始化啊~未初始化的值是未定義的,啥結果可能都有。

能夠看出:

區別關鍵就是訪問控制,struct默認是public,class默認是private。包括struct下定義的屬性/成員訪問控制(默認public),繼承方式默認public。幾個注意的地方,struct還能繼承class,class也能繼承struct,必定條件下class也能像struct用{…}初始化構造.當struct/class帶有OO特性時,如繼承、構造函數、虛函數時,除了默認的訪問控制符外,struct跟class行爲徹底同樣。例子中的經過{…}提供參數化列表構造一個實例,class也能經過這樣的方式構造。當有繼承、構造函數等OO特性定義(非成員函數)時,即使是struct也不能經過{…}初始化構造.

另外,class在c++中還能在模版定義中,相似(typename),而struct不行。

以上算是struct和class的區別和聯繫吧。核心思想是記住c++中的struct也能用於OOP,與class的默認訪問控制權限不同。

Reference : http://blog.sina.com.cn/s/blog_48f587a80100k630.html

C# struct和class區別: http://www.cnblogs.com/gsk99/archive/2011/05/20/1904552.html

相關文章
相關標籤/搜索