Cpp 结构体

 1 /* Cpp 结构体 */
 2 
 3 #include<iostream>
 4 
 5 struct lstruct
 6 {
 7     int num;
 8 };
 9 
10 // c++ 结构体会给默认的值
11 struct MyStruct
12 {
13     int num;
14     double db = 10.8;// 默认的值
15     
16     MyStruct sx;// 结构体内部不能定义自己
17     
18     MyStruct *pnext;
19     MyStruct *phead;
20 
21     lstruct l1;
22     
23     void boss()
24     {
25     
26     }
27 };
28 
29 struct MyStructA
30 {
31     int num;
32     double db = 10.8;// 默认的值
33     
34     MyStruct sx;// 结构体内部不能定义自己
35     
36     MyStruct *pnext;
37     MyStruct *phead;
38 
39     lstruct l1;
40     
41     void boss()
42     {
43     
44     }
45 };
46 
47 
48 struct 
49 {
50     int num;
51     double db;
52     MyStruct boss1;
53 }sx,sy;// 匿名结构体不允许初始化
54 
55 int main()
56 {
57     MyStruct s1;
58 
59     std::cout << s1.db << std::endl;
60     
61 
62     std::cin.get();
63     return 0;
64 }
65 
66 //----------------------------------------------------
67 
68 int main()
69 {
70     MyStruct s1;// 自动管理
71     MyStruct *pnew = new MyStruct;// 堆上 手动管理
72     
73     s1.l1.num;
74     pnew->l1.num;
75     (*pnew).l1.num;
76 
77     // 类型相同可以整体赋值
78     MyStruct s2(s1);// 结构体c++风格初始化方式
79 
80     MyStruct *pnew2(new MyStruct);
81     
82     MyStructA sa1;
83     MyStruct s3(sal);// 不行  C++强类型 必须类型匹配
84 
85     std::cin.get();
86     return 0;
87 }

 

posted on 2015-06-02 16:50  Dragon-wuxl  阅读(456)  评论(0)    收藏  举报

导航