c++ 类名和enum时重复时要在类名前加class::
一些不好的习惯都是用小写,但又没有区分开token,看看代码再说,下面的代码是我在测试polymorphism时写的一部分,怎么也查不出,最后主意到下面红色标志出来的语句,他们(animal)重复了,要区分开来。
重复名的有很多情况,以后遇见再在一起总结,先记录下来。
- #include <iostream>
- #include <cstring>
- #include <string>
- using namespace std;
- enum zoo_obj_kind{
- null = 0,
- #define zk_null (zoo_obj_kind(null))
- no = 0,
- #define zk_no (zoo_obj_kind(no))
- animal = 1,
- #define zk_animal (zoo_obj_kind(animal))
- plant = 2,
- #define zk_plant (zoo_obj_kind(plant))
- others = 3,
- #define zk_others (zoo_obj_kind(others))
- max = 4
- #define zk_max 4
- };
- static const char * zoo_kind_str [zk_max ] ={
- "null",
- "animal",
- "plant",
- "others"
- };
- class obj{
- private:
- char name [40];
- // void *other_msg;
- public:
- obj() {
- strcpy(name,"null") ;
- }
- obj(char *nm){
- strncpy(name,!nm?"null":nm,sizeof(name));
- }
- void say(){
- cout << "name:" << name << endl;
- }
- void say(obj *obj){
- !obj
- ? cout << "null\n"
- : cout << "name:" << obj->name << endl;
- }
- };
- class zoo_obj{
- private:
- zoo_obj_kind z_kind;
- char name [40];
- void *other_msg;
- public:
- zoo_obj() {
- z_kind = null;
- strcpy(name,"null") ;
- }
- zoo_obj(char *nm, zoo_obj_kind k){
- strncpy(name,!nm?"null":nm,sizeof(name));
- z_kind = k;
- }
- void say(){
- cout << "name:" << name << ",kind:"
- << zoo_kind_str[(int) z_kind] << endl;
- }
- void say(zoo_obj *obj){
- !obj
- ? cout << "null\n"
- : cout << "name:" << obj->name << endl;
- }
- };
- class animal:public obj{
- private:
- int lags;
- public:
- animal(char *nm, int l) :lags(l), obj(nm){ }
- void say(){
- obj::say();
- cout << "lag:" << lags << endl;
- }
- };
- int main(void){
- zoo_obj obj = zoo_obj( "cat", zoo_obj_kind(animal));
- obj.say();
- class::animal dog ("joel's dog",4);
- dog.say();
- }