当前位置:   article > 正文

C++中struct的使用_c++struct用法

c++struct用法

C++语言继承了C语言的struct,并且加以扩充。在C语言中struct是只能定义数据成员,而不能定义成员函数的。而在C++中,struct类似于class,在其中既可以定义数据成员,又可以定义成员函数。

结构类型是用户定义的复合类型,它可由不同类型的字段或成员构成。在C++中,struct与class基本是通用的,唯一不同的是如果使用class关键字,类中定义的成员变量或成员函数默认都是private属性的,而采用struct关键字,结构体中定义的成员变量或成员函数默认都是public属性的。

         在C中,必须显示使用struct关键字来声明结构。在C++中,不需要在定义该类型之后使用struct关键字。可以选择在定义结构类型时,通过在右大括号和分号之间放置一个或多个逗号分隔的变量名称来声明变量。可以初始化结构变量,每个变量的初始化必须括在大括号中。

         Differences between C struct & C++ struct:

(1)、 C structure can't contain functions means only data members are allowed, but structure in C++ can have both functions &  data members.

(2)、 struct keyword is necessary in C to create structure type variable, but it is  redundant & not necessary in C++.

(3)、 Size of empty structure is undefined behavior in C, but it is always 1 in C++.

(4)、 Structure in C can't have static members, but C++ structure can have static members.

(5)、 Structure members can't be directly initialized inside the struct in C, but it is allowed in C++ since C++11.

(6)、 We can have both pointers and references to struct in C++, but only pointers to structs are allowed. (References aren't feature of C language).

(7)、 C++ also have .* and -> operators to access individual members of struct, but C doesn't have such kind of operators.

(8)、 struct declaration establishes a  scope in C++ and not in C, which makes member enumerators and nested structs possible in C++(you can *write* them inside a struct in C, but they escape and become local to whatever function the parent struct is in).

(9)、 In C, we need to use struct tag whenever we declare a struct variable. In C++, the struct tag is not necessary.

(10)、C++ structures are very similar to a class, with the only difference being that in a class, all members are private by default. But in a C++ structure, all members are public by default. In C, there is no concept of public or private.

(11)、C++ structures can have member functions, whereas C structures cannot.

(12)、You can have constructors, destructors, copy constructors and so on in C++ structures. You cannot in C structures.

(13)、C++ structures can have static members, whereas C structures cannot.

下面是从其他文章中copy的测试代码,详细内容介绍可以参考对应的reference:

  1. #include "struct.hpp"
  2. #include <cstring>
  3. #include <cstdlib>
  4. #include <iostream>
  5. ///
  6. // reference: https://msdn.microsoft.com/zh-cn/library/64973255.aspx
  7. struct PERSON { // Declare PERSON struct type
  8. int age; // Declare member types
  9. long ss;
  10. float weight;
  11. char name[25];
  12. } family_member; // Define object of type PERSON
  13. struct CELL { // Declare CELL bit field
  14. unsigned short character : 8; // 00000000 ????????
  15. unsigned short foreground : 3; // 00000??? 00000000
  16. unsigned short intensity : 1; // 0000?000 00000000
  17. unsigned short background : 3; // 0???0000 00000000
  18. unsigned short blink : 1; // ?0000000 00000000
  19. } screen[25][80]; // Array of bit fields
  20. int test_struct1()
  21. {
  22. struct PERSON sister; // C style structure declaration
  23. PERSON brother; // C++ style structure declaration
  24. sister.age = 13; // assign values to members
  25. brother.age = 7;
  26. std::cout << "sister.age = " << sister.age << '\n';
  27. std::cout << "brother.age = " << brother.age << '\n';
  28. CELL my_cell;
  29. my_cell.character = 1;
  30. std::cout << "my_cell.character = " << my_cell.character<<'\n';
  31. return 0;
  32. }
  33. //
  34. // reference: http://www.tutorialspoint.com/cplusplus/cpp_data_structures.htm
  35. struct Books {
  36. char title[50];
  37. char author[50];
  38. char subject[100];
  39. int book_id;
  40. };
  41. void printBook(struct Books book)
  42. {
  43. std::cout << "Book title : " << book.title << std::endl;
  44. std::cout << "Book author : " << book.author << std::endl;
  45. std::cout << "Book subject : " << book.subject << std::endl;
  46. std::cout << "Book id : " << book.book_id << std::endl;
  47. }
  48. int test_struct2()
  49. {
  50. struct Books Book1; // Declare Book1 of type Book
  51. struct Books Book2; // Declare Book2 of type Book
  52. // book 1 specification
  53. strcpy(Book1.title, "Learn C++ Programming");
  54. strcpy(Book1.author, "Chand Miyan");
  55. strcpy(Book1.subject, "C++ Programming");
  56. Book1.book_id = 6495407;
  57. // book 2 specification
  58. strcpy(Book2.title, "Telecom Billing");
  59. strcpy(Book2.author, "Yakit Singha");
  60. strcpy(Book2.subject, "Telecom");
  61. Book2.book_id = 6495700;
  62. // Print Book1 info
  63. printBook(Book1);
  64. // Print Book2 info
  65. printBook(Book2);
  66. return 0;
  67. }
  68. ///
  69. // reference: http://www.dummies.com/how-to/content/how-to-build-a-structure-template-in-c.html
  70. template<typename T>
  71. struct Volume {
  72. T height;
  73. T width;
  74. T length;
  75. Volume()
  76. {
  77. height = 0;
  78. width = 0;
  79. length = 0;
  80. }
  81. T getvolume()
  82. {
  83. return height * width * length;
  84. }
  85. T getvolume(T H, T W, T L)
  86. {
  87. height = H;
  88. width = W;
  89. length = L;
  90. return height * width * length;
  91. }
  92. };
  93. int test_struct3()
  94. {
  95. Volume<int> first;
  96. std::cout << "First volume: " << first.getvolume() << std::endl;
  97. first.height = 2;
  98. first.width = 3;
  99. first.length = 4;
  100. std::cout << "First volume: " << first.getvolume() << std::endl;
  101. Volume<double> second;
  102. std::cout << "Second volume: " << second.getvolume(2.1, 3.2, 4.3) << std::endl;
  103. std::cout << "Height: " << second.height << std::endl;
  104. std::cout << "Width: " << second.width << std::endl;
  105. std::cout << "Length: " << second.length << std::endl;
  106. return 0;
  107. }
  108. ///
  109. // reference: http://www.java2s.com/Code/Cpp/Class/Constructoranddestructorinsideastruct.htm
  110. struct StringClass
  111. {
  112. StringClass(char *ptr);
  113. ~StringClass();
  114. void show();
  115. private:
  116. char *p;
  117. int len;
  118. };
  119. StringClass::StringClass(char *ptr)
  120. {
  121. len = strlen(ptr);
  122. p = (char *)malloc(len + 1);
  123. if (!p) {
  124. std::cout << "Allocation error\n";
  125. exit(1);
  126. }
  127. strcpy(p, ptr);
  128. }
  129. StringClass::~StringClass()
  130. {
  131. std::cout << "Freeing p\n";
  132. free(p);
  133. }
  134. void StringClass::show()
  135. {
  136. std::cout << p << " - length: " << len;
  137. std::cout << std::endl;
  138. }
  139. int test_struct4()
  140. {
  141. StringClass stringObject1("www.java2s_1.com."), stringObject2("www.java2s_2.com.");
  142. stringObject1.show();
  143. stringObject2.show();
  144. return 0;
  145. }

GitHub:  https://github.com/fengbingchun/Messy_Test

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家自动化/article/detail/714938
推荐阅读
相关标签
  

闽ICP备14008679号