lnlidawei

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

cpp:  指针和引用(class & object)

 

 

 

 

一、指针使用、引用的运用

 

 

  1、指针具有双重赋值的属性:第一重赋值,为指针变量赋值内存地址;第二重赋值,为指针变量所指的内存的存储空间赋予内容。

 

 

 

 

  2、(引用:cpp的特性;“引用”作为函数的参数):"引用"仍然是值传递。和普通变量相比较,“引用”只是不产生变量的临时副本。

 

 1 [root@rockylinux tmp]# uname -a
 2 Linux rockylinux 4.18.0-372.19.1.el8_6.x86_64 #1 SMP Tue Aug 2 16:19:42 UTC 2022 x86_64 x86_64 x86_64 GNU/Linux
 3 [root@rockylinux tmp]# 
 4 [root@rockylinux tmp]# 
 5 [root@rockylinux tmp]# g++ --version
 6 g++ (GCC) 8.5.0 20210514 (Red Hat 8.5.0-10)
 7 Copyright (C) 2018 Free Software Foundation, Inc.
 8 This is free software; see the source for copying conditions.  There is NO
 9 warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10 
11 [root@rockylinux tmp]# 
12 [root@rockylinux tmp]# 
13 [root@rockylinux tmp]# cat reference.cpp 
14 #include<iostream>
15 
16 
17 using namespace std;
18 
19 
20 int* return_value1(int& para)
21 {
22         int* tmp = &para;  // 可以获取para的地址;
23         return tmp;
24 }
25 
26 
27 int* return_value2(int& para)
28 {
29         return &para;   // 可以返回para的地址;
30 }
31 
32 
33 int* return_value3(int& para)
34 {
35         return para;    // 如果这里报错,就说明“‘引用’是值传递”;
36 }
37 
38 
39 int main(int argc, char* argv[], char* envp[])
40 
41 int main(int argc, char* argv[], char* envp[])
42 {
43 
44         int ref = 1949;
45 
46 
47         int* pt1 = nullptr;
48         int* pt2 = nullptr;
49         int* pt3 = nullptr;
50 
51 
52         pt1 = return_value1(ref);
53         cout << "pt1 = " << *pt1 << endl;
54         pt2 = return_value2(ref);
55         cout << "pt2 = " << *pt2 << endl;
56         pt3 = return_value3(ref);
57         cout << "pt3 = " << *pt3 << endl;
58 
59 
60         return 0;
61 
62 
63 }
64 [root@rockylinux tmp]# 
65 [root@rockylinux tmp]# 
66 [root@rockylinux tmp]# g++ -o reference reference.cpp 
67 reference.cpp: In function ‘int* return_value3(int&)’:
68 reference.cpp:22:9: error: invalid conversion fromint’ to ‘int*’ [-fpermissive]
69   return para; // 如果这里报错,就说明“‘引用’是值传递”;
70          ^~~~
71 reference.cpp: At global scope:
72 reference.cpp:28:1: error: expected initializer before ‘int73  int main(int argc, char* argv[], char* envp[])
74  ^~~
75 [root@rockylinux tmp]# 
76 [root@rockylinux tmp]# 

 

 

 

 

二、代码和运行结果

 

  1 [root@rockylinux tmp]# uname -a
  2 Linux rockylinux 4.18.0-372.19.1.el8_6.x86_64 #1 SMP Tue Aug 2 16:19:42 UTC 2022 x86_64 x86_64 x86_64 GNU/Linux
  3 [root@rockylinux tmp]# 
  4 [root@rockylinux tmp]# 
  5 [root@rockylinux tmp]# g++ --version
  6 g++ (GCC) 8.5.0 20210514 (Red Hat 8.5.0-10)
  7 Copyright (C) 2018 Free Software Foundation, Inc.
  8 This is free software; see the source for copying conditions.  There is NO
  9 warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 10 
 11 [root@rockylinux tmp]# 
 12 [root@rockylinux tmp]# 
 13 [root@rockylinux tmp]# cat mix_data_structure.cpp 
 14 #include<iostream>
 15 #include<string>
 16 
 17 
 18 using namespace std;
 19 
 20 
 21 template<typename N, typename I>
 22 struct persion{
 23         string uid;
 24         N uname;
 25         I uinformation;
 26 };
 27 
 28 
 29 class msg
 30 {
 31 public:
 32         msg()
 33         {
 34                 uid = new string;
 35                 uname = new string;
 36                 uinformation = new string;
 37         }
 38         msg(string* id, string* name, string* info)
 39         { 
 40                 uid = new string;
 41                 uname = new string;
 42                 uinformation = new string;
 43 
 44                 *uid=*id; 
 45                 *uname=*name;
 46                 *uinformation=*info;
 47         }
 48         msg(const msg* message)
 49         {
 50                 uid = new string;
 51                 uname = new string;
 52                 uinformation = new string;
 53 
 54                 *uid = *message->uid;
 55                 *uname = *message->uname;
 56                 *uinformation = *message->uinformation;
 57         }
 58         ~msg(){ delete uid; delete uname; delete uinformation;}
 59 
 60 
 61         void set_uid(string* id)
 62         {
 63                 *uid=*id;
 64         }
 65 
 66         string* get_uid()
 67         {
 68                 return uid;
 69         }
 70 
 71 
 72         void set_uname(string* name)
 73         {
 74                 *uname=*name;
 75         }
 76 
 77         string* get_uname()
 78         {
 79                 return uname;
 80         }
 81 
 82         void set_uinformation(string* info)
 83         {
 84                 *uinformation=*info;
 85         }
 86 
 87         string* get_uinformation()
 88         {
 89                 return uinformation;
 90         }
 91 private:
 92         string* uid;
 93         string* uname;
 94         string* uinformation;
 95 };
 96 
 97 
 98 template<class N, class I>
 99 class student
100 {
101 public:
102         student(){ stud.uid="usr_null"; stud.uname="usr_null"; stud.uinformation="usr_null";}
103         student(string* id, N* name, I* info){ stud.uid=*id; stud.uname=*name; stud.uinformation=*info;}
104         student(const student& st){ stud.uid=st.uid; stud.uname=st.uname; stud.uinformation=st.uinformation;}
105         void set_all(string id, N name, I info){ stud.uid=id; stud.uname=name, stud.uinformation=info;}
106         string get_uid(){ return stud.uid;}
107         N get_uname(){ return stud.uname;}
108         I get_uinformation(){ return stud.uinformation;}
109         void print_result()
110         {
111                 cout << "OS_STUDENT:";
112                 cout << "\tid=" << stud.uid;
113                 cout << ",\tname=" << stud.uname;
114                 cout << ",\tinformation=" << stud.uinformation << endl;
115         }
116 private:
117         persion<N,I> stud;
118 };
119 
120 
121 class teacher
122 {
123 public:
124         teacher(){ message = new msg;}
125         teacher(string* id, string* name, string* info)
126         { 
127                 message= new msg; 
128                 message->set_uid(id);
129                 message->set_uname(name); 
130                 message->set_uinformation(info);
131         }
132         teacher(const teacher& t)
133         { 
134                 message = new msg;
135                 message ->set_uid(t.message->get_uid()); 
136                 message ->set_uname(t.message->get_uname());
137                 message ->set_uinformation(t.message->get_uinformation());
138         }
139         ~teacher(){delete message;}
140         void print_result()
141         {
142                 cout <<"OS_TEACHER:";
143                 cout <<"\tid=" << *message->get_uid();
144                 cout <<",\tname="<< *message->get_uname();
145                 cout <<",\tinformation="<< *message->get_uinformation()<< endl;
146         }
147 private:
148         msg* message;
149 };
150 
151 
152 
153 int main(int argc, char* argv[], char* envp[])
154 {
155 
156         string id = "20220830";
157         string name = "tiger";
158         string info = "forest_universe";
159 
160         string* pointer_id = &id;
161         string* pointer_name = &name;
162         string* pointer_info = &info;
163 
164 
165         student<string, string> st(pointer_id, pointer_name, pointer_info);
166         st.print_result();
167 
168 
169         teacher tr(pointer_id, pointer_name, pointer_info);
170         tr.print_result();
171 
172 
173         return 0;
174 
175 
176 }
177 [root@rockylinux tmp]# 
178 [root@rockylinux tmp]# 
179 [root@rockylinux tmp]# g++ -o mix_data_structure mix_data_structure.cpp 
180 [root@rockylinux tmp]# 
181 [root@rockylinux tmp]# 
182 [root@rockylinux tmp]# ./mix_data_structure 
183 OS_STUDENT:     id=20220830,    name=tiger,     information=forest_universe
184 OS_TEACHER:     id=20220830,    name=tiger,     information=forest_universe
185 [root@rockylinux tmp]# 
186 [root@rockylinux tmp]# 

 

 

posted on 2022-08-30 18:51  lnlidawei  阅读(364)  评论(0编辑  收藏  举报