2

と呼ばれる抽象クラスがあるとしVehicleます。

class Vehicle {
    virtual bool raceWith(Vehicle *anotherVehicle) = 0;
};

そして、そのサブクラスBicycleCar:

//  forward declaration
class Car;

class Bicycle : public Vehicle {
    virtual bool raceWith(Vehicle *anotherVehicle) {
        throw SomeExceptionClass();
    }

    virtual bool raceWith(Car *anotherVehicle) {
        return true;
    }

    virtual bool raceWith(Bicycle *anotherVehicle) {
        return false;
    }
};

ただし、このコード ブロックは SomeExceptionClass をスローします。

Vehicle *aBicycle = new Bicycle();
Vehicle *aCar = new Car();

aBicycle->raceWith(aCar);

ここで何をしますか?C++ では、このようにポリモーフィック メソッドを使用できませんか?

どんな助けでも大歓迎です。ありがとう。

編集dynamic_cast<>:とdecltypeバリアントで回答を提供することも良いでしょうか?

4

3 に答える 3

4

Bicycle以下は aと aを競合させVehicleます:

Vehicle *aBicycle = new Bicycle();
Vehicle *aCar = new Car();

aBicycle->raceWith(aCar);

Bicycleaと aを競合させるには、Carダブル ディスパッチを使用する必要があります。

ダブル ディスパッチを機能させるには、thisポインタを使用して次の関数を呼び出し、2 番目の仮想呼び出しが正しいタイプの車両に解決されるようにします。

class Car;
class Bicycle;

class Vehicle {
public:
    virtual bool raceWith(Vehicle& anotherVehicle) = 0;
    virtual bool raceWith(Bicycle& anotherVehicle) = 0;
    virtual bool raceWith(Car& anotherVehicle) = 0;
};


class Bicycle : public Vehicle {
public:
    virtual bool raceWith(Vehicle& anotherVehicle) override
    {
        //throw std::exception();
        return anotherVehicle.raceWith(*this);
    }

    virtual bool raceWith(Car& anotherVehicle)
    {
        return true;
    }

    virtual bool raceWith(Bicycle& anotherVehicle)
    {
        return false;
    }
};

class Car : public Vehicle {
public:
    virtual bool raceWith(Vehicle& anotherVehicle) override
    {
        return true;
    }

    virtual bool raceWith(Car& anotherVehicle) override
    {
        return true;
    }

    virtual bool raceWith(Bicycle& anotherVehicle) override
    {
        return false;
    }

};

int main()
{

    Vehicle *aBicycle = new Bicycle();
    Vehicle  *aCar = new Car();

    aBicycle->raceWith(*aCar);
}

return anotherVehicle.raceWith(*this);2 番目の仮想呼び出しを実行する に注意してください。

次に、関数は次の順序で呼び出されます。

  • main();
  • Bicycle::raceWith(Vehicle& anotherVehicle);
  • Car::raceWith(Bicycle& anotherVehicle);

以下は同じプログラムですが、質問で提供されているポインターと例外の使用に固執しています。

#include <exception>

class Car;
class Bicycle;

class Vehicle {
public:
    virtual bool raceWith(Vehicle* anotherVehicle) = 0;
    virtual bool raceWith(Bicycle* bicycle) = 0;
    virtual bool raceWith(Car* car) = 0;
};


class Bicycle : public Vehicle {
public:
    virtual bool raceWith(Vehicle* anotherVehicle) override
    {
        //throw std::exception();
        return anotherVehicle->raceWith(this);
    }

    virtual bool raceWith(Car* car) override
    {
        return true;
    }

    virtual bool raceWith(Bicycle* bicycle) override
    {
        return false;
    }
};

class Car : public Vehicle {
public:
    virtual bool raceWith(Vehicle* anotherVehicle) override
    {
        throw std::exception();
    }

    virtual bool raceWith(Car* car) override
    {
        return true;
    }

    virtual bool raceWith(Bicycle* bicycle) override
    {
        return false;
    }

};

int main()
{

    Vehicle *aBicycle = new Bicycle();
    Vehicle  *aCar = new Car();

    aBicycle->raceWith(aCar);
}
于 2016-04-21T18:30:56.173 に答える