[cpp]: class/struct -- 初始化‘实例对象’

发布时间 2024-01-11 00:53:08作者: lnlidawei

[cpp]: class/struct -- 初始化‘实例对象’

 

 

 

 

一、说明

 

  1、编译标准:  std = c++20

 

  2、编译语句:  g++ -std=c++20 -O2 -Wall -pedantic -pthread main.cpp && ./a.out

 

 

 

 

二、class/struct(初始化‘实例对象’)

 

  1、class/struct 定义和初始化:

 1 // class/struct defined
 2 class/struct object
 3 {
 4 public:
 5     object(){}
 6     object(string s ): name(s){}
 7     object(string s, string t) :name{s}, type{t}{}
 8 private:
 9     string name;
10     string type;
11 };
12 
13 
14 // class/struct initialization
15 string s="sun-wukong";
16 string t="monkey";
17 
18 
19 // o1() -> object()
20 object o1();
21 
22 
23 // o2() -> object(string s)
24 object o2(s);
25 
26 
27 // o3() -> object(string s)
28 object o3{t};
29 
30 
31 // o4() -> object(string s, string t)
32 object o3{s, t};

 

 

二、源程序

 1 #include <iostream>
 2 #include <string>
 3 #include <vector>
 4 
 5 
 6 using namespace std;
 7 
 8 
 9 class user_wt{
10 public:
11 
12     // init object: user_wt o1(); user_wt o2{}
13     user_wt(){}
14 
15     // init object: user_wt o3(s,t); user_wt o4{s,t};
16     user_wt(string s, string t):name(s), type(t){}
17 
18     // init object: user_wt o5{x,y,z};
19     user_wt(string x, string y, string z):name{x}, type{y}{}
20 
21     void out(){ cout << "\nname := " << name << endl; cout << "type := " << type << endl; }
22     void out_order(int i){ cout << "\nname_"<< i << " := " << name << endl; cout << "type_"<< i << " := " << type << endl; }
23     
24 private:
25     string name = "USER_NULL";
26     string type = "USER_NULL";
27 };
28 
29 template<class T>
30 void
31 init(string *x, string *y, T *t)
32 {
33     for(int i=0; i<4; i++)
34     {
35         t[i] = user_wt(x[i], y[i]);    
36     }
37 }
38 
39 template<class U>
40 void
41 output(U *u)
42 {
43     for(int j=0; j<4; j++)
44     {
45         u[j].out_order(j);  
46     }
47 }
48 
49 
50 void run()
51 {
52 
53     string s[] = {"sun-wukong", "zhu-wuneng", "sha-wujing", "tang-xuanzang" };
54     string t[] = {"monkey", "pig", "unknown", "human" };
55     user_wt o[4];
56     
57     init<user_wt>(s, t, o);
58     output<user_wt>(o);
59     
60     
61     string u = "sun-wukong";
62     string v = "monkey";
63     string w = "whatis";
64     
65     user_wt w1;
66     w1.out();
67     
68     user_wt w2(u,v);
69     w2.out();
70     
71     // 'w3{}' calls 'user_wt::user_wt()' to initialization;  w3{} CALL user_wt::user_wt();
72     user_wt w3{};
73     w3.out();
74     
75     // 'w4{u, v, w}' call 'user_wt::user_wt(string x, string y, string z)' for initialization.
76     user_wt w4{u,v,w};
77     w4.out();
78 
79 }
80 
81 
82 int main()
83 {
84     run();
85     
86     return 0;
87 }

 

 

三、运行结果

 1 name_0 := sun-wukong
 2 type_0 := monkey
 3 
 4 name_1 := zhu-wuneng
 5 type_1 := pig
 6 
 7 name_2 := sha-wujing
 8 type_2 := unknown
 9 
10 name_3 := tang-xuanzang
11 type_3 := human
12 
13 name := USER_NULL
14 type := USER_NULL
15 
16 name := sun-wukong
17 type := monkey
18 
19 name := USER_NULL
20 type := USER_NULL
21 
22 name := sun-wukong
23 type := monkey

 

 

四、参考资料

 

  1、 Constructors and member initializer lists  ----  https://en.cppreference.com/w/cpp/language/constructor

 

  2、  cpp online tools  ----  https://coliru.stacked-crooked.com/