C ++使用枚举调用基础构造函数 [英] C++ Call base constructor with enum

查看:740
本文介绍了C ++使用枚举调用基础构造函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



例如:

  enum CarBrand 
{
Volkswagen,
Ferrari,
Bugatti
}

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

class FerrariCar:public Car
{
public:
//为什么我不允许这样做?
FerrariCar(int horsePower):Car(horsePower,CarBrand.Ferrari){}
〜FerrariCar();
};

因为我在编译一些沿着示例行的时候遇到以下错误:预期的主表达式'。'token



任何帮助将不胜感激。

Ferrari 。


Is it possible to pass a value and a constant enum to the base constructor of a class?

For example:

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

Because I'm getting the following error when compiling something along the lines of the example: expected primary-expression before ‘.’ token

Any help would be appreciated!

解决方案

In C++, you do not prefix the enum value with the enum type name. Just say Ferrari.

这篇关于C ++使用枚举调用基础构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆