gpt4 book ai didi

C++ 使用枚举调用基础构造函数

转载 作者:太空狗 更新时间:2023-10-29 19:42:33 24 4
gpt4 key购买 nike

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

例如:

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

关于C++ 使用枚举调用基础构造函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4639873/

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