我是否需要使用继承对象(相对于基对象)覆盖我的虚函数?

Do I need to override my virtual function using an inherited object (vs. base object)?

本文关键字:对象 覆盖 我的 函数 相对于 是否 继承      更新时间:2023-10-16

假设我有一个基类class Base,以及两个派生类Derived: public BaseDerived2: public Base。我有一个纯虚函数

virtual int CompareByInsertKey( Base* item_in_list ) = 0;

Base类。所以它是一个函数,它接受一个指向Base类的指针。

我希望我的类Derived覆盖这个虚拟函数,但我希望它以一个指向Derived类的指针作为参数。我也希望我的类Derived2覆盖这个函数,但我想把一个指向Derived2类的指针作为参数。如果我把参数保持为Base* item_in_list,因为DerivedDerived2Base类的一部分,它会自动做到这一点吗?如果不是,我该怎么做?

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

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

virtual int CompareByInsertKey( Base* item_in_list ) override
{
    Derived *ptr = dynamic_cast<Derived *>(item_in_list);
    // ...
}

编译器不能做你希望做的事。

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

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

试着运行下面的例子。

#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;
}

KC

您可以通过更复杂的结构和关于

类型的知识来做到这一点
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 a and b '是相同的,那么派生函数实际上会被执行吗,这是您在寻找的功能吗?