不能在C ++中的派生类中重载基类方法

时间:2014-08-24 03:27:06

标签: c++ methods overloading base-class

我想做什么

我正在转换现有的代码库,该代码库使用dynamic_casting来识别派生类(形状)以将派生类特定处理应用于基于访问者模式的方案。为此,我在一个基类(虚拟)和每个派生类中添加了一个processMe方法,并为一个ShapeProcessor类添加了一个handleShape方法,一个用于每种要处理的形状,一个典型的访问者模式。我有一个ShapeProcessor抽象基类,它有一个纯虚方法,强制用户提供一个捕获所有形状的处理器,并允许用户从ShapeProcessor派生并根据需要添加其他形状处理方法(例如MyShapeProcessor:public ShapeProcessor)

观察

然而,发现只有在myShapeProcessor中为所有形状调用catch all方法,我的形状特定方法不会被调用。如何根据需要调整特定于形状的方法,我需要做什么?警告:如果我将所有处理程序方法放在一个类中,它可以正常工作。这是否意味着不可能重载基类中的方法?我已经阅读了有关名称隐藏的帖子,但这似乎不适用于此处。或者是吗?我尝试使用“使用”取消隐藏基类方法,但似乎没有帮助。

这是伪代码示例:

class Shape {
    virtual void processMe (ShapeProcessor * sp) {
        sp->processShape (*this);
}

// User derived shape
class Circle : public Shape {
    void processMe (ShapeProcessor * sp) {
        sp->processShape (*this);
}

// Base ShapeProcessor
class ShapeProcessor {
    virtual void processShape (Shape& shape) = 0; // User must provide a catch all method
}

// User provided shape processor
class MyShapeProcessor : public ShapeProcessor {
    void processShape (Circle& circle) {
       // Never gets called, even for Circle objects!
    }
    void processShape (Shape& shape) {
       // Always gets called for all shapes!
       cout << "Unsupported shape!" << endl;
    }
}

// User code
Circle * circle = new Circle();
MyShapeProcessor * sp = new MyShapeProcessor();
circle->processMe (sp);  

// Expecting processMe to eventually call MyShapeProcessor processShape (Circle) but calls processShape (Shape)

// Caveat: If I get rid of the ShapeProcessor base class and if I put all shape handles in a single class
// it works fine. Does this mean that it is not possible to overload methods in the base class? I have read
// the posts on name hiding, but that does not seem to apply here. Or does it?

2 个答案:

答案 0 :(得分:2)

您需要添加:

void processShape (Circle& circle);

ShapeProcessor作为virtual成员函数。没有它,任何processMe函数都无法看到此函数。

class ShapeProcessor {
    virtual void processShape (Shape& shape) = 0;

    virtual void processShape (Circle& circle) = 0;

};

在项目中添加Shape的新子类型时,您必须返回ShapeProcessor并为子类型添加新的processShape函数。

您可以使用模板来避免这种紧密耦合。看看https://stackoverflow.com/a/7877397/434551

答案 1 :(得分:0)

这种方法也适合你吗?

#include <iostream>


class Shape;


class ShapeProcessor {
public:
    virtual void processShape(Shape &shape){
        std::cout<<"Shape Processor";
    }
};

class CircleProcessor: public ShapeProcessor {

public:
    void processShape(Shape &shape)
    {
        std::cout<<"CircleProcessor";
    }
};
class  Shape
{
public:

    virtual void processMe(ShapeProcessor *sp){
        sp->processShape(*this);
    }
};
class Circle:public Shape
{
public:
    void processMe(ShapeProcessor *sp){
        sp->processShape(*this);
    }
};

int main(int argc, const char * argv[])
{
    Shape  *circle = new Circle();
    ShapeProcessor *sp = new CircleProcessor();
    circle->processMe(sp);
    return 0;
}