我是否需要使用继承对象(相对于基对象)覆盖我的虚函数?
Do I need to override my virtual function using an inherited object (vs. base object)?
假设我有一个基类class Base
,以及两个派生类Derived: public Base
和Derived2: public Base
。我有一个纯虚函数
virtual int CompareByInsertKey( Base* item_in_list ) = 0;
在Base
类。所以它是一个函数,它接受一个指向Base
类的指针。
我希望我的类Derived
覆盖这个虚拟函数,但我希望它以一个指向Derived
类的指针作为参数。我也希望我的类Derived2
覆盖这个函数,但我想把一个指向Derived2
类的指针作为参数。如果我把参数保持为Base* item_in_list
,因为Derived
和Derived2
是Base
类的一部分,它会自动做到这一点吗?如果不是,我该怎么做?
谢谢,如果你需要我发布更多信息,请评论。
它自动获取指向派生类的指针,因为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 '是相同的,那么派生函数实际上会被执行吗,这是您在寻找的功能吗?
相关文章:
- "main"函数堆栈中的对象在第一个任务运行时被覆盖 (FreeRTOS)
- 专门化模板覆盖函数/避免对象切片
- 为什么销毁被放置 new 覆盖的对象不是未定义的行为?
- 当覆盖映射中的复杂对象键时,旧对象将被删除(C++)
- 我想覆盖运算符'='但是在重载之后,运算符没有将正确的信息传递给对象
- 为什么我的类节点覆盖自身而不是创建新的节点对象
- 调用Main将替换矢量对象并覆盖数据
- 错误消息:不允许抽象类类型 "X" 的对象:纯虚拟"Y"没有覆盖器
- C ++如何创建一个即使对象被覆盖也会持续存在的指针?
- 使用放置 new 覆盖内存中的对象
- 是否可以在不手动在C 中的每个派生类中添加覆盖的克隆方法来克隆多态对象
- 抽象类型的对象 - 覆盖不起作用吗?
- 覆盖组合对象的<<运算符会给出错误 C++
- 如何在Windows中注册COM(组件对象模型)?(用于图标覆盖处理程序)
- 通过使用覆盖操作员的代理来同步对象
- 派生类对象覆盖对象向量中的基类函数
- 3d对象覆盖-增强现实irrlight + opencv
- 我是否需要使用继承对象(相对于基对象)覆盖我的虚函数?
- vector的最后一个对象覆盖所有对象
- 比较同一类的 2 个对象(覆盖 == 运算符)c++