码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2015-09-04 07:24:33      阅读:150      评论:0      收藏:0      [点我收藏+]

标签:

 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. }

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

标签:

原文地址:http://www.cnblogs.com/timdes/p/4781106.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!