是否可以将值和常量枚举传递给类的基本构造函数?

例如:

enum CarBrand
{
    Volkswagen,
    Ferrari,
    Bugatti
};

class Car
{
    public:
      Car(int horsePower, CarBrand brand)
      {
          this->horsePower = horsePower;
          this->brand = brand;
      }
      ~Car() { }
    private:
      int horsePower;
      CarBrand brand;
 };

 class FerrariCar : public Car
 {
     public:
       // Why am I not allowed to do this ..?
       FerrariCar(int horsePower) : Car(horsePower, CarBrand.Ferrari) { }
       ~FerrariCar();
 };

因为在按照示例进行编译时出现以下错误:expected primary-expression before ‘.’ token
任何帮助,将不胜感激!

最佳答案

在C++中,不要在枚举值前添加枚举类型名称。只需说Ferrari即可。

10-04 11:10