从C到C++——结构体(struct)的涅槃
从C到C++,结构体(struct)涅槃重生,那么,C语言的结构体和C++结构体有什么不同呢,下面是一个测试程序,程序中大量的注释就算作本文的讲解吧!
1 /*- ========================================================== 2 * 文件名 :TestCppStruct.cpp 3 * 开发人员:袁培荣 4 * 当前版本:1.0.0.2595 5 * 创建时间:2012-05-17 6 * 修改时间:2012-05-17 7 * 功能说明:C++结构体功能测试 8 * 版权说明:版权所有 袁培荣 YuanPeirong 9 * 编译环境:Windows 7(x64) SP1 简体中文专业版 10 * 编译器: Visual Studio 2010 SP1(中文旗舰版) 11 MinGW 20111108 12 Visual C++ 6.0 SP6(中文企业版) 13 - ==========================================================*/ 14 15 #include <iostream> 16 using std::cout; 17 using std::endl; 18 19 //==================================================================== 20 struct STest 21 { 22 int a; 23 char c; 24 STest() 25 { 26 cout<<"STest结构体构造函数被执行"<<endl; 27 a=10; 28 c='S'; 29 }; 30 }; 31 //==================================================================== 32 struct STest2 33 { 34 int a; 35 char c; 36 STest2(); 37 }; 38 39 STest2::STest2() 40 { 41 cout<<"STest2结构体构造函数被执行"<<endl; 42 a=15; 43 c='T'; 44 } 45 //==================================================================== 46 struct STest3 47 { 48 int a; 49 char c; 50 void Test() 51 { 52 cout<<"STest3结构体Test函数被执行"<<endl; 53 a=10; 54 c='S'; 55 }; 56 }; 57 //==================================================================== 58 struct STest4 59 { 60 int a; 61 char c; 62 void Test(); 63 }; 64 65 void STest4::Test() 66 { 67 cout<<"STest4结构体Test函数被执行"<<endl; 68 a=15; 69 c='T'; 70 } 71 //==================================================================== 72 struct STest5 73 { 74 int a; 75 void Test(); 76 private: 77 char c; 78 void Test2(); 79 80 }; 81 82 void STest5::Test() 83 { 84 cout<<"STest5结构体Test函数被执行"<<endl; 85 a=16; 86 c='X'; 87 } 88 89 void STest5::Test2() 90 { 91 cout<<"STest5结构体Test2函数被执行"<<endl; 92 a=17; 93 c='Y'; 94 } 95 //==================================================================== 96 int main(int argc, char* argv[]) 97 { 98 //==================================================================== 99 STest s11; //在C语言中应为 struct STest s11; 在C++中struct可省略 100 STest2 s21; // 101 cout<<"s11: "<<s11.a<<" "<<s11.c<<endl; 102 cout<<"s21: "<<s21.a<<" "<<s21.c<<endl; 103 s11.a=100; 104 s21.c='Z'; 105 cout<<"直接修改了s11和s21的值!"<<endl; 106 cout<<"s11: "<<s11.a<<" "<<s11.c<<endl; 107 cout<<"s21: "<<s21.a<<" "<<s21.c<<endl; 108 //==================================================================== 109 // C++的结构体有构造函数,C语言中结构体不能有函数,更别谈构造函数 110 //==================================================================== 111 112 //==================================================================== 113 // STest s12={1,'A'}; 114 // STest s22={2,'B'}; 115 // cout<<"s12: "<<s12.a<<" "<<s12.c<<endl; 116 // cout<<"s22: "<<s22.a<<" "<<s22.c<<endl; 117 //==================================================================== 118 // error: `s12' must be initialized by constructor, not by `{...}' 119 // error: `s22' must be initialized by constructor, not by `{...}' 120 // 结构体一旦有构造函数就不能通过 {...} 方式初始化 121 //==================================================================== 122 123 //==================================================================== 124 STest3 s31={1,'A'}; 125 STest4 s41={2,'B'}; 126 cout<<"通过 {...} 方式初始化 了s31和s41的值"<<endl; 127 cout<<"s31: "<<s31.a<<" "<<s31.c<<endl; 128 cout<<"s41: "<<s41.a<<" "<<s41.c<<endl; 129 s31.Test(); 130 s41.Test(); 131 cout<<"s31: "<<s31.a<<" "<<s31.c<<endl; 132 cout<<"s41: "<<s41.a<<" "<<s41.c<<endl; 133 s31.a=100; 134 s41.c='Z'; 135 cout<<"直接修改了s31和s41的值!"<<endl; 136 cout<<"s31: "<<s31.a<<" "<<s31.c<<endl; 137 cout<<"s41: "<<s41.a<<" "<<s41.c<<endl; 138 //==================================================================== 139 // C++的结构体可以有成员函数 140 // C++的结构体没有构造函数时可以通过 {...} 方式初始化 141 //==================================================================== 142 143 144 //==================================================================== 145 //STest5 s51={5,'A'}; 146 // error: `s51' must be initialized by constructor, not by `{...}' 147 148 //s51.Test2(); 149 //error: `void STest5::Test2()' is private 150 151 //s51.c='Z'; 152 // error: `char STest5::c' is private 153 154 STest5 s52; 155 s52.a=52; 156 cout<<"s52.a="<<s52.a<<endl; 157 s52.Test(); 158 cout<<"s52.a="<<s52.a<<endl; 159 s52.a=10; 160 cout<<"s52.a="<<s52.a<<endl; 161 //==================================================================== 162 // 结构体STest5的测试说明: 163 // C++中可以为结构体的成员设定访问控制级别 private public protected 164 // C++中一旦为结构体设定访问控制级别,那么这个结构体其实就是类 165 // 所以不可以通过 {...} 方式初始化 166 // 类中的访问控制级别和结构体是一致的,但是缺省时: 167 // 类中是 private 而结构体中是 public 168 // 这是唯一的区别 169 //==================================================================== 170 171 return 0; //在C语言和早期C++中此句不可以省略,在标准C++中此句可省略 172 } 173 174 //==================================================================== 175 //运行结果 176 //==================================================================== 177 // STest结构体构造函数被执行 178 // STest2结构体构造函数被执行 179 // s11: 10 S 180 // s21: 15 T 181 // 直接修改了s11和s21的值! 182 // s11: 100 S 183 // s21: 15 Z 184 // 通过 {...} 方式初始化 了s31和s41的值 185 // s31: 1 A 186 // s41: 2 B 187 // STest3结构体Test函数被执行 188 // STest4结构体Test函数被执行 189 // s31: 10 S 190 // s41: 15 T 191 // 直接修改了s31和s41的值! 192 // s31: 100 S 193 // s41: 15 Z 194 // s52.a=52 195 // STest5结构体Test函数被执行 196 // s52.a=16 197 // s52.a=10 198 //==================================================================== 199 // 总结: C++的结构体可以分两部分来看 200 // 1.保持和C语言兼容的结构体,不涉及函数 201 // 2.C++支持的结构体,这种结构类事实上和类class功能相同,但有一点区别 202 // 区别在于 203 // 类中默认的访问控制级别为 private 204 // 结构体中默认的访问控制级别为 public 205 // 是的,我们可以在编程时用 private public protected来修饰结构体中的成员 206 // C++的结构体就是C语言的结构体和C++中的类的综合体 207 //==================================================================== 208 209 //==================================================================== 210 // 以上测试的错误信息是在MinGW编译器上编译时复制的 211 // 以上代码在 212 // Visual Studio 2010 SP1(中文旗舰版) 213 // MinGW 20111108 214 // Visual C++ 6.0 SP6(中文企业版) 215 // 这三个编译器上编译通过 216 //====================================================================

浙公网安备 33010602011771号