cpp:' data_type * '被视为一个数据类型
一、结论
1、data_type *,它被视为一个数据类型;
2、data_type *,用于定义指针变量;
3、举例:
int * pt = nullptr; // int* 被视为一个数据类型;
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]# g++ --version
5 g++ (GCC) 8.5.0 20210514 (Red Hat 8.5.0-10)
6 Copyright (C) 2018 Free Software Foundation, Inc.
7 This is free software; see the source for copying conditions. There is NO
8 warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
9
10 [root@rockylinux tmp]#
11 [root@rockylinux tmp]# cat data_pointer.cpp
12 /*
13 *
14 * file_name = data_pointer.cpp
15 * date = 2022-08-30
16 *
17 *
18 */
19
20
21 #include<iostream>
22
23
24 using namespace std;
25
26
27 // 定义类
28 class __data_structure{
29 public:
30 __data_structure(){ elem=0;}
31 __data_structure(int t){ elem=t;}
32 __data_structure(const __data_structure& ds){ elem = ds.elem; }
33 void set_element(int t){ elem=t;}
34 int get_element(){ return elem;}
35 void msg(){ cout << "element = " << elem << endl;}
36
37 private:
38 int elem;
39 };
40
41
42 // 定义数据类型
43 // typedef __data_structure* pointer_data_structure, data_structure;
44 typedef __data_structure data_structure, *pointer_ds;
45
46
47 template<typename T>
48 T return_back(T t)
49 {
50 return t;
51 }
52
53
54 int main(int argc, char* argv[], char* envp[])
55 {
56
57 data_structure keep_return_value;
58 pointer_ds pt_ds1=nullptr, pt_ds2=nullptr, keep_return_pointer=nullptr;
59 data_structure ds1(30);
60 data_structure ds2(60);
61 pt_ds1 = &ds1;
62 pt_ds2 = &ds2;
63
64
65 //定义的类测试
66 // pt_ds1->msg();
67
68
69 // parameter = value
70 cout << "OS: keep_return_value " << endl;
71 keep_return_value = return_back(ds2);
72 keep_return_value.msg();
73
74
75 // parameter = pointer
76 cout << "OS: keep_return_pointer " << endl;
77 keep_return_pointer = return_back(pt_ds2);
78 keep_return_pointer -> msg();
79
80
81 /* 说明:
82 *
83 * 1、return_back()函数返回的类型是T。
84 * 2、当函数return_back()的参数类型是__data_structure, 返回值的类型是__data_structure;
85 * 3、当函数return_back()的参数类型是__data_structure*, 返回值的类型是__data_structure*;
86 *
87 * 结论:因为“__data_structure”和“__data_structure*”都是使用“类型的参数T”返回的值,
88 * 所以“ __data_structure* ”被视为“数据类型”。
89 *
90 * 疑问:拷贝构造函数(__data_structure(const __data_structure& ds) )为什么可以直接调用private权限的变量elem(ds.elem )?
91 *
92 */
93
94
95 return 0;
96 }
97 [root@rockylinux tmp]#
98 [root@rockylinux tmp]#
99 [root@rockylinux tmp]# g++ -o data_pointer data_pointer.cpp
100 [root@rockylinux tmp]#
101 [root@rockylinux tmp]#
102 [root@rockylinux tmp]# ./data_pointer
103 OS: keep_return_value
104 element = 60
105 OS: keep_return_pointer
106 element = 60
107 [root@rockylinux tmp]#
108 [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 parameter_function.cpp
14 /*
15 * date: 2022-08-30
16 * info: testing a function parameter of a function.
17 */
18
19 #include<iostream>
20
21
22 using namespace std;
23
24
25 // define funciton1 to be called.
26 void msg1()
27 {
28 cout << "CPP: hello, world!" << endl;
29 }
30
31
32 // define funciton2 to be called.
33 int msg2(int var)
34 {
35 return var;
36 }
37
38
39 // define funciton3 to be called.
40 int* msg3(int* pt)
41 {
42 return pt;
43 }
44
45
46 // define calling function1.
47 void cpp_fun1( void(*fun1)())
48 {
49 fun1();
50 cout <<"CPP: cpp_fun1" << endl;
51 }
52
53
54 // define calling function2, template function.
55 template<typename T>
56 T cpp_fun2( T pt, T (*fun2)(T))
57 {
58 fun2(pt);
59 cout <<"CPP: cpp_fun2" << endl;
60 return fun2(pt);
61 }
62
63
64 // testing part.
65 int main(int argc, char* argv[], char* envp[])
66 {
67
68 int keep_var, var = 40;
69 int* pt_cpp_fun2_parameter, *keep_pt;
70 pt_cpp_fun2_parameter = &var;
71
72
73 // call cpp_fun1
74 cpp_fun1(msg1);
75
76
77 // call cpp_fun2
78 keep_var = cpp_fun2(var, msg2);
79 cout <<"CPP: cpp_fun2_return_general_value = " << keep_var << endl;
80
81
82 // call cpp_fun2 with pointer parameter;
83 keep_pt = cpp_fun2(pt_cpp_fun2_parameter, msg3);
84 cout <<"CPP: cpp_fun2_return_pt_value = " << *keep_pt << endl;
85
86
87 return 0;
88 }
89 [root@rockylinux tmp]#
90 [root@rockylinux tmp]#
91 [root@rockylinux tmp]# g++ -o parameter_function parameter_function.cpp
92 [root@rockylinux tmp]#
93 [root@rockylinux tmp]#
94 [root@rockylinux tmp]# ./parameter_function
95 CPP: hello, world!
96 CPP: cpp_fun1
97 CPP: cpp_fun2
98 CPP: cpp_fun2_return_general_value = 40
99 CPP: cpp_fun2
100 CPP: cpp_fun2_return_pt_value = 40
101 [root@rockylinux tmp]#
102 [root@rockylinux tmp]#
本文由 lnlidawei 原创、整理、转载,本文来自于【博客园】; 整理和转载的文章的版权归属于【原创作者】; 转载或引用时请【保留文章的来源信息】:https://www.cnblogs.com/lnlidawei/p/16639263.html