lnlidawei

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

[c][cpp]:  c和cpp的命名规则(变量、函数、类、结构、枚举、共用)

 

 

 

 

一、说明:为了更好识别变量、函数、类、结构体,因此制定命名规则

 

  1、全局变量的命名规则:  g_<变量名>

 

  2、函数的命名规则:  f_<函数名>

 

  3、【class(类)】的命名规则:  c_<类名>

    4.1、类的成员变量的命名规则:  mv_<变量名>

    4.2、类的成员函数的命名规则:  mf_<函数名>

    4.3、类的静态变量的命名规则:  sv_<变量名>

    4.4、类的静态函数的命名规则:  sf_<函数名>

 

  4、【struct(结构)】的命名规则:  s_<结构名>

    4.1、结构的成员变量的命名规则:  mv_<变量名>

    4.2、结构的成员函数的命名规则:  mf_<函数名>

    4.3、结构的静态变量的命名规则:  sv_<变量名>

    4.4、结构的静态函数的命名规则:  sf_<函数名>

 

  5、【enum(枚举)】的命名规则:  e_<枚举名>

 

  6、【union(共用)】的命名规则:  u_<结构名>

    4.1、共用的成员变量的命名规则:  mv_<变量名>

    4.2、共用的成员函数的命名规则:  mf_<函数名>

    4.3、共用的静态变量的命名规则:  sv_<变量名>

    4.4、共用的静态函数的命名规则:  sf_<函数名>

    4.5、【共用】特别说明:  在【共用】中,可以定义函数(cpp;std=c++23)。【参考文献: https://en.cppreference.com/w/cpp/language/union  】

 1 //  c++
 2 //  g++ -std=c++20 -O2 -Wall -pedantic -pthread main.cpp && ./a.out
 3 
 4 #include <iostream>
 5 #include <string>
 6  
 7 union u_S
 8 {
 9     u_S()
10     { 
11         std::cout << "\n[os]#\tunion::S()" << std::endl;
12     }
13     
14     u_S(std::string s):mv_str(s){}
15     
16     ~u_S() 
17     { 
18         std::cout << "\n[os]#\tunion::~S()" << std::endl;
19     }
20     
21     void mf_msg() 
22     { 
23         std::cout << "\n[os]#\tstr := " << mv_str << std::endl;
24     }
25     
26     void mf_tell()
27     { 
28         std::cout << "\n[os]#\ts::tell()" << std::endl;
29     }
30     
31     int mf_work()
32     { 
33         std::cout << "\n[os]#\twork:1" <<std::endl;
34         return 1;
35     }
36     
37     static void sf_big()
38     {
39         std::cout << "\n[os]#\ts::sf_big() := " << u_S::sv_big << std::endl;
40     }
41     
42     std::string mv_str;
43     
44     static int sv_big;
45 
46 };
47 
48 int u_S::sv_big = 345;
49  
50 int main()
51 {
52     u_S s = {"Hello, world"};
53     s.mf_msg();
54     s.mf_tell();
55     s.mf_work();
56     u_S::sf_big();
57     
58     return 0;
59 }
60 
61 
62 /****  result  ****/
63 
64 [os]#    str := Hello, world
65 
66 [os]#    s::tell()
67 
68 [os]#    work:1
69 
70 [os]#    s::sf_big() := 345
71 
72 [os]#    union::~S()

 

二、代码

  1 /*
  2 *    file_name  :=  ruler_of_name.cpp
  3 *    date  :=  2024-01-24
  4 *
  5 *
  6 *    the ruler of name:
  7 *
  8 *        1.  global name of variable  =  g_<variable_name>
  9 *
 10 *        2.  a name of general function  =  f_<function_name>
 11 *
 12 *        3.  class
 13 *            3.1  a name of class type:    c_<class_name>
 14 *            3.2  variable name of class members:    mv_<variable_name>
 15 *            3.3  function name of class members:    mf_<function_name>
 16 *            3.4  static variable name of class:    sv_<variable_name>
 17 *            3.5  static function name of class:    sf_<function_name>
 18 *
 19 *        4.  struct
 20 *            4.1  a name of struct type:    s_<struct_name>
 21 *            4.2  variable name of struct members:    mv_<variable_name>
 22 *            4.3  function name of struct members:    mf_<function_name>
 23 *            4.4  static variable name of struct:    sv_<variable_name>
 24 *            4.5  static function name of struct:    sf_<function_name>
 25 *
 26 *        5.  enum
 27 *            5.1  a name of enum type:    e_<enum_name>
 28 *
 29 *        6.  union
 30 *            6.1  a name of union type:    u_<union_name>
 31 *            6.2  variable name of union members:    mv_<variable_name>
 32 *            6.3  function name of union members:    mf_<function_name>
 33 *            6.4  static variable name of union:    sv_<variable_name>
 34 *            6.5  static function name of union:    sf_<function_name>
 35 *
 36 */
 37 
 38 #include <iostream>
 39 #include <string>
 40 
 41 using namespace std;
 42 
 43 //  global variables name  =  g_<variable_name>
 44 string g_os  =  "Linux Fedora" ;
 45 string g_prompt  =  "[os]#\t" ;
 46 
 47 //  a name of general function  =  f_<function_name>
 48 void f_msg()
 49 {
 50     cout << "[os]#\tHello, World!" << endl ;    
 51 }
 52 
 53 //  a name of class type  =  c_<class_name>
 54 //  variable name of class members =  mv_<variable_name>
 55 //  function name of class members =  mf_<function_name>
 56 //  static variable name of class =  sv_<variable_name>
 57 //  static function name of class =  sf_<function_name>
 58 class c_object      //  class name  =  c_<class_name>
 59 {
 60 private:
 61     //  variable name of class members =  mv_<variable_name>
 62     string mv_name = "" ;
 63     
 64 public:
 65     c_object(){}
 66     
 67     //  function name of class members =  mf_<function_name>
 68     void mf_msg() {}
 69     
 70     //  static variable name of class =  sv_<variable_name>
 71     static int sv_count ;
 72     
 73     //  static function name of class =  sf_<function_name>
 74     static void sf_tell() 
 75     {
 76         cout << "[os]#\tsf_tell()" << endl;    
 77     } 
 78 };
 79 int c_object::sv_count  =  0 ;
 80 
 81 //  a name of struct type  =  s_<struct_name>
 82 struct s_msg
 83 {
 84     //  variable name of struct type  =  mv_<variable_name>
 85     string   mv_name  =  "" ;
 86 
 87     //  function name of struct type  =  mf_<function_name>
 88     void mf_msg()
 89     {
 90         cout << "[os]#\ts_msg::mf_msg()" << endl ;
 91     }
 92 
 93     //  static variable name of struct  =  sv_<variable_name>
 94     static string sv_name ;
 95     //  static function name of struct  =  sf_<function_name>
 96     static void sf_work()
 97     {
 98         cout << "[os]#\ts_msg::sf_work("<< s_msg::sv_name <<")" << endl ;
 99     }
100 };
101 string s_msg::sv_name = "s_msg::sf_name" ;
102 
103 // enum name  =  e_<enum_name>
104 enum e_color
105 {
106     RED ,
107     YELLOW ,
108     BLUE
109 };
110 
111 // print e_color object
112 void f_print_msg_ecolor(e_color clr)
113 {
114     //e_color clr = RED ;
115     cout << "[os]#\t" << "f_print_msg_ecolor(" << clr << ")" << endl ;
116 }
117 
118 //  union name  =  u_<union_name>
119 union u_persion
120 {
121     // constructer of union
122     u_persion()
123     {
124         string msg  =  "u_persion()" ;
125         cout << "[os]#\t" << msg << endl ;
126     }
127     u_persion(string name):mv_name(name)
128     {
129         string msg  =  "u_persion(string name)" ;
130         cout << "[os]#\t" << msg << endl ;
131     }
132 
133     // destructer of union
134     ~u_persion()
135     {
136         string msg  =  "~u_persion()" ;
137         cout << "[os]#\t" << msg << endl ;
138     }
139 
140     // a function name of union  =  mf_<function_name>
141     void  mf_speak()
142     {
143         string msg  =  "mf_speak(), " ;
144         cout << "[os]#\t" << msg << mv_name << endl ;
145     }
146 
147     // a static function name of union  =  sf_<function_name>
148     static void sf_work()
149     {
150         string msg  =  "sf_work(),  " ;
151         cout << "[os]#\t" << msg << u_persion::sv_count << endl ;
152     }
153 
154     // a variable name of union  =  mv_<variable_name>
155     string mv_name ;
156 
157     // a static variable name of union  =  sv_<variable_name>
158     static int sv_count ;
159 };
160 int u_persion::sv_count = 360 ;
161 
162 //  a name of a function  =  f_<function_name>
163 void f_run()
164 {
165     cout << g_prompt << g_os << endl ;
166 
167     //  class
168     c_object::sf_tell() ;
169     c_object co ;
170     co.mf_msg() ;
171 
172     // struct
173     s_msg sm ;
174     sm.mf_msg() ;
175 
176     //  enum
177     e_color clr = RED ;
178     f_print_msg_ecolor(clr) ;
179 
180     //  union
181     u_persion::sf_work();
182     u_persion up{"sun-wukong"};
183     up.mf_speak();
184    
185 }
186 
187 //  the entrance of this procedure
188 int main()
189 {
190     f_run();
191     return 0;    
192 }

 

三、运行结果

 1 g++ -std=c++20 -O2 -Wall -pedantic -pthread main.cpp && ./a.out
 2 
 3 
 4 [os]#    Linux Fedora
 5 [os]#    sf_tell()
 6 [os]#    s_msg::mf_msg()
 7 [os]#    f_print_msg_ecolor(0)
 8 [os]#    sf_work(),  360
 9 [os]#    u_persion(string name)
10 [os]#    mf_speak(), sun-wukong
11 [os]#    ~u_persion()

 

四、参考文档

 

  1、  union declaration  --  https://en.cppreference.com/w/cpp/language/union

 

posted on 2024-01-25 01:18  lnlidawei  阅读(100)  评论(0编辑  收藏  举报