技术头条 - 一个快速在微博传播文章的方式     搜索本站
您现在的位置首页 --> 编程语言 --> struct与class区别联系

struct与class区别联系

浏览:3780次  出处信息

注意C中的struct和C++中的struct是不一样的,c中的struct比较原生,仅仅是将一些属性封装起来构成一个整体,没有OO的相关特性。而c++中的struct是对c中的struct进行扩展(兼容c中的struct),具备OO的特性,其实c++中的class能干的事情struct几乎都能干什么继承、多态等都OK。直接看下面代码,不同编译器对结果可能不一样:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#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);
   }
};

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

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(){}
};


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);
   }
};


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

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);

   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结果

不同编译器结果不一样主要是体现在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的默认访问控制权限不一样。

建议继续学习:

  1. 标签?ID?还是CLASS?    (阅读:4342)
QQ技术交流群:445447336,欢迎加入!
扫一扫订阅我的微信号:IT技术博客大学习
© 2009 - 2024 by blogread.cn 微博:@IT技术博客大学习

京ICP备15002552号-1