其他分享
首页 > 其他分享> > 如果只有指向C中基类的指针,则使用派生类参数重载函数

如果只有指向C中基类的指针,则使用派生类参数重载函数

作者:互联网

我见过人们使用指向基类的指针容器来保存共享相同虚函数的对象组.是否可以使用这些基类指针来使用派生类的重载函数.很难解释我的意思,但(我认为)很容易用代码显示

class PhysicsObject // A pure virtual class
{
    // Members of physics object
    // ...
};

class Circle : public PhysicsObject
{
    // Members of circle
    // ...
};

class Box : public PhysicsObject
{
    // Members of box
    // ...
};

// Overloaded functions (Defined elsewhere)
void ResolveCollision(Circle& a, Box& b);
void ResolveCollision(Circle& a, Circle& b);
void ResolveCollision(Box& a, Box& b);

int main()
{
    // Container to hold all our objects 
    std::vector<PhysicsObject*> objects;

    // Create some circles and boxes and add to objects container
    // ...

    // Resolve any collisions between colliding objects
    for (auto& objA : objects)
        for (auto& objB : objects)
            if (objA != objB)
                ResolveCollision(*objA, *objB); // !!! Error !!! Can't resolve overloaded function
}

我的第一个想法是让这些函数成为虚拟类成员(如下所示),但我很快意识到它有完全相同的问题.

class Circle;
class Box;
class PhysicsObject // A pure virtual class
{
    virtual void ResolveCollision(Circle& a) = 0;
    virtual void ResolveCollision(Box& a) = 0;
    // Members of physics object
    // ...
};

class Box;
class Circle : public PhysicsObject
{
    void ResolveCollision(Circle& a);
    void ResolveCollision(Box& a);
    // Members of circle
    // ...
};

class Circle;
class Box : public PhysicsObject
{
    void ResolveCollision(Circle& a);
    void ResolveCollision(Box& a);
    // Members of box
    // ...
};

从谷歌搜索问题似乎可能它可以使用强制转换解决但我无法弄清楚如何找到正确的类型转换(也很难看).我怀疑我问的是错误的问题,并且有一种更好的方法来构建我的代码,它可以避开这个问题并获得相同的结果.

解决方法:

使用双重调度,它将是这样的:

class Circle;
class Box;

// Overloaded functions (Defined elsewhere)
void ResolveCollision(Circle& a, Box& b);
void ResolveCollision(Circle& a, Circle& b);
void ResolveCollision(Box& a, Box& b);
class PhysicsObject // A pure virtual class
{
public:
    virtual ~PhysicsObject() = default;

    virtual void ResolveCollision(PhysicsObject&) = 0;
    virtual void ResolveBoxCollision(Box&) = 0;
    virtual void ResolveCircleCollision(Circle&) = 0;
};

class Circle : public PhysicsObject
{
public:
    void ResolveCollision(PhysicsObject& other) override { return other.ResolveCircleCollision(*this); }
    void ResolveBoxCollision(Box& box) override { ::ResolveCollision(*this, box);}
    void ResolveCircleCollision(Circle& circle) override { ::ResolveCollision(*this, circle);}
    // ...
};

class Box : public PhysicsObject
{
public:
    void ResolveCollision(PhysicsObject& other) override { return other.ResolveBoxCollision(*this); }
    void ResolveBoxCollision(Box& box) override { ::ResolveCollision(box, *this);}
    void ResolveCircleCollision(Circle& circle) override { ::ResolveCollision(circle, *this);}
    // ...
};

标签:derived-class,c,polymorphism
来源: https://codeday.me/bug/20190823/1699176.html