gpt4 book ai didi

c++ - C++ 中的虚方法重写

转载 作者:搜寻专家 更新时间:2023-10-31 00:10:46 25 4
gpt4 key购买 nike

假设我们有一个名为 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变体?

最佳答案

以下将用 VehicleBicycle 比赛:

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

aBicycle->raceWith(aCar);

要让自行车汽车比赛,我们需要使用双重调度。

为了使双重调度工作,我们使用 this 指针来调用下一个函数,以便第二个虚拟调用可以解析为正确的车辆类型:

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); 将执行第二个虚拟调用。

然后按以下顺序调用函数:

  • 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);
}

关于c++ - C++ 中的虚方法重写,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36777483/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com