c++ 类名和enum时重复时要在类名前加class::

 c++ 类名和enum时重复时要在类名前加class::

一些不好的习惯都是用小写,但又没有区分开token,看看代码再说,下面的代码是我在测试polymorphism时写的一部分,怎么也查不出,最后主意到下面红色标志出来的语句,他们(animal)重复了,要区分开来。

重复名的有很多情况,以后遇见再在一起总结,先记录下来。

  1. #include <iostream>
  2. #include <cstring>
  3. #include <string>
  4. using namespace std;
  5. enum zoo_obj_kind{
  6.         null = 0,
  7. #define zk_null (zoo_obj_kind(null))
  8.         no = 0,
  9. #define zk_no (zoo_obj_kind(no))
  10.         animal = 1,
  11. #define zk_animal (zoo_obj_kind(animal))
  12.         plant = 2,
  13. #define zk_plant (zoo_obj_kind(plant))
  14.         others = 3,
  15. #define zk_others (zoo_obj_kind(others))
  16.         max = 4
  17. #define zk_max 4
  18. };
  19. static const char * zoo_kind_str [zk_max ] ={
  20.         "null",
  21.         "animal",
  22.         "plant",
  23.         "others"
  24. };
  25. class obj{
  26.         private:
  27.                 char name [40];
  28.         // void *other_msg;
  29.         public:
  30.                 obj() {
  31.                         strcpy(name,"null") ;
  32.                 }
  33.                 obj(char *nm){
  34.                         strncpy(name,!nm?"null":nm,sizeof(name));
  35.                 }
  36.                 void say(){
  37.                         cout << "name:" << name << endl;
  38.                 }
  39.                 void say(obj *obj){
  40.                         !obj
  41.                                 ? cout << "null\n"
  42.                                 : cout << "name:" << obj->name << endl;
  43.                 }
  44. };
  45. class zoo_obj{
  46.         private:
  47.                 zoo_obj_kind z_kind;
  48.                 char name [40];
  49.                 void *other_msg;
  50.         public:
  51.                 zoo_obj() {
  52.                         z_kind = null;
  53.                         strcpy(name,"null") ;
  54.                 }
  55.                 zoo_obj(char *nm, zoo_obj_kind k){
  56.                         strncpy(name,!nm?"null":nm,sizeof(name));
  57.                         z_kind = k;
  58.                 }
  59.                 void say(){
  60.                         cout << "name:" << name << ",kind:"
  61.                                 << zoo_kind_str[(int) z_kind] << endl;
  62.                 }
  63.                 void say(zoo_obj *obj){
  64.                         !obj
  65.                                 ? cout << "null\n"
  66.                                 : cout << "name:" << obj->name << endl;
  67.                 }
  68. };
  69. class animal:public obj{
  70.         private:
  71.                 int lags;
  72.         public:
  73.                 animal(char *nm, int l) :lags(l), obj(nm){ }
  74.                 void say(){
  75.                         obj::say();
  76.                         cout << "lag:" << lags << endl;
  77.                 }
  78. };
  79. int main(void){
  80.         zoo_obj obj = zoo_obj( "cat", zoo_obj_kind(animal));
  81.         obj.say();
  82.         class::animal dog ("joel's dog",4);
  83.         dog.say();
  84. }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值