代码之家  ›  专栏  ›  技术社区  ›  pasha

是否可以为复制基的虚拟函数提供不同的定义?

  •  0
  • pasha  · 技术社区  · 9 年前

    我知道该声明, “复制基类的虚拟函数可以被派生类中的(单个)函数重写。” 我很想知道是否可以提供一个以上的定义,以防正确定义和调用?

    为了更清楚, 我的类和类层次结构如下

         A  A
         |  |
         B  C
         \ /
          D
    
    //i.e., I am not deriving B and C from a virtual base A.
    
    class A {
        virtual void f(){
             cout<<"belongs to A"<<endl;
        }
    }
    class B: public A {
        void f(){
             cout<<"belongs to B"<<endl;
        }
    }
    class C: public A {
        void f(){
             cout<<"belongs to C"<<endl;
        }
    }
    class D: public B, public C {
        void f(){  //This overrides definitions B::f() and C::f()
             cout<<"belongs to D"<<endl;
        }
    }
    B* b;
    C* c;
    D d;
    b = &d;
    c = &d;
    b->f();//This would output "belongs to D"
    c->f();//This also would output "belongs to D"
    
    //Now I want to redefine f() in D like this
    class D: public B, public C {
        void B::f() {
             cout<<"belongs to D::B::f()"<<endl;
        }
        void C::f() {
             cout<<"belongs to D::C::f()"<<endl;
        }
    }
    //So that 
    B* b;
    C* c;
    D d;
    b = &d;
    c = &d;
    b->f();//This would output "belongs to D::B::f()"
    c->f();//This also would output "belongs to D::C::f()"
    
    3 回复  |  直到 9 年前
        1
  •  2
  •   Guillaume Gris    9 年前

    在某些时候,您需要能够告诉编译器您要使用哪个实现。

    这样做的一种方法是:

      A
     / \
    B   C
     \ /
      D
    
    struct A
    {
      virtual void f();
    };
    
    struct B : public A
    {
      void f() override;
    };
    
    struct C : public A
    {
      void f() override;
    };
    
    struct D : public B,C
    {
    };
    
    int test(int num) {
    
      D d;
      d.f();    // undefined
      d.A::f(); // Impossible because conversion from D to A is ambiguous
      d.B::f(); // calls B implementation
      d.C::f(); // calls C implementation
    }
    
        2
  •  0
  •   aliasm2k    9 年前

    下面是一个虚拟函数的基本示例

    class Person
    {
        int age;
        public:
            Person(int=0);
            virtual void describe() = 0;
            int getAge();
    };
    
    Person :: Person(int age)
    {
        this->age = age;
    }
    
    int Person :: getAge()
    {
        return this->age;
    }
    
    class Student : public Person
    {
        public:
            Student(int=0);
            void describe();
    };
    
    Student :: Student(int age) : Person(age) {}
    void Student :: describe() 
    {
        std::cout << "Hi, I am a student, ";
        std::cout << this->getAge() << " years old!";
    }
    
    class Employee : public Student
    {
        public:
            Employee(int=0);
            void describe();
    };
    
    Employee :: Employee(int age) : Person(age) {}
    void Employee :: describe()
    {
        std::cout << "Hi, I am an employee, ";
        std::cout << this->getAge() << " years old!";
    }
    
        3
  •  0
  •   pasha    9 年前

    简短的答案是 .好奇的家伙

    答案由@curiousguy在评论中提供。