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

我是否需要使用继承的对象(而不是基对象)重写我的虚拟函数?

  •  0
  • ASASAQQQQ  · 技术社区  · 10 年前

    说我有基地 class Base 和两个派生类 Derived: public Base Derived2: public Base . 我有一个纯粹的虚拟功能

    virtual int CompareByInsertKey( Base* item_in_list ) = 0;
    

    在课堂上 Base 。所以它是一个函数 基础

    我要我的课 Derived 覆盖此虚拟函数,但我希望它将指针指向 派生的 类作为参数。 我也想要我的课 Derived2 要覆盖此函数,但我想获取指向 衍生2 类作为参数。 如果我将参数保持为 Base* item_in_list 因为 派生的 衍生2 是一部分 基础 课程? 如果没有,我该怎么做?

    谢谢,如果你需要我发布更多信息,请评论。

    4 回复  |  直到 10 年前
        1
  •  2
  •   programmerjake    10 年前

    它自动获取指向派生类的指针,因为 Derived * 可隐式转换为 Base * 。您必须在函数参数中使用Base来实现它,以便覆盖该函数。

    virtual int CompareByInsertKey( Base* item_in_list ) override
    {
        Derived *ptr = dynamic_cast<Derived *>(item_in_list);
        // ...
    }
    
        2
  •  2
  •   R Sahu    10 年前

    编译器无法执行所希望的操作。

    struct Base
    {
       virtual int CompareByInsertKey( Base* item ) = 0;
    };
    
    struct Derived : Base
    {
       virtual int CompareByInsertKey( Base* item )
       {
          return 1;
       }   
    };
    
    struct Derived2 : Base
    {
       virtual int CompareByInsertKey( Base* item )
       {
          return 2;
       }   
    };
    
    int main()
    {
        Base* d1 = new Derived;
        Base* d2 = new Derived2;
    
        d1->CompareByInsertKey(d2); // This is a valid call
                                    // but the argument is not of type Derived*
    }
    
        3
  •  0
  •   KC-NH    10 年前

    为了使函数签名匹配,必须在CompareByInsertKey中使用Base*。不幸的是,这意味着您可以在Derived2上传递Derived作为参数。也许模板对你的应用更有效。

    尝试运行以下示例。

    #include "stdafx.h"
    using namespace std;
    
    
    
    class Base {
    public:
        string name;
        Base(string n) : name(n) {}
        virtual int CompareByInsertKey(Base* item_in_list) = 0;
        virtual string Name() { return name;  }
    
    };
    
    class Derived : public Base
    {
    public:
        Derived() : Base("derived") {}
        int CompareByInsertKey(Base *item_in_list)
        {
            cout << "In Derived, parameter is " << item_in_list->Name() << endl;
            return 0;
        }
    };
    
    
    class Derived2 : public Base
    {
    public:
        Derived2() : Base("derived2") {}
    
        int CompareByInsertKey(Base *item_in_list)
        {
            cout << "In Derived2, parameter is " << item_in_list->Name() << endl;
    
            return 0;
        }
    
    };
    
    
    
    int _tmain(int argc, _TCHAR* argv[])
    {
        Base * d2 = new Derived2();
        Base *d = new Derived();
        d2->CompareByInsertKey(d);
    
        return 0;
    }
    

    千卡

        4
  •  0
  •   Harald Scheirich    10 年前

    您可以通过更复杂的结构和类型知识来实现这一点

    class Base
    {
    public:
        int publicComparator(Base* instance)
        {
            // you can use either typeid() or your on enum that you put in the 
            // hierarchy
            if (typeid(this) == typeidf(instance))
            {
                return privateComparator(instance);
            }
    
            return ErrorCode;
        }
    
    private:
        virtual privateComparator(Base *instance) = 0;
    }
    
    class Dervied : public Base {
    
    private:
        privateComparator(Base* instance) override {
            // The assumption is privateComparator is only called when this cast
            // can sucessfully be completed
            Derived* derivedInstance = static_cast<Derived*>(instance);
    
            // Do functionality
    
            return result;
        }
    }
    

    所以如果你打电话 a->publicComparator(b)' only when the type of and b`是否相同?派生函数是否会被实际执行,这是您正在寻找的功能吗?