【问题标题】:How to store different objects, that are of the same parent, in one array? C++如何在一个数组中存储具有相同父级的不同对象? C++
【发布时间】:2021-08-14 00:27:09
【问题描述】:

我有一个 Vheicle 类,它是子类 Bus、Helicopter、Train。 有没有办法将所有 Bus、Helicopter、Train 对象存储在一个数组中? 我在互联网上查看并没有找到任何可行的解决方案。

这是我的代码:

类声明:

class Vehicle {
private:
    string vehicleID;
    string man;
public:
    Vehicle(string, string);
    Vehicle();

    virtual ~Vehicle();
};

class Bus: public Vehicle
{
public:
    int currentMileage;
    Bus(string, string, int);
    Bus();

    virtual ~Bus();


};
class Train: public Vehicle
{
public:
    char motorType;
    Train(string, string, char);
    Train();

    virtual~Train();
};


Vehicle::Vehicle(string id, string m) {
    vehicleID = id;
    man = m;
}

Bus::Bus(string id, string m, int curMile): Vehicle{id,m}{
    currentMileage = curMile;
}

Train::Train(string id, string m, char mType):Vehicle(id,m){
    motorType = mType;
}

我需要这个工作:

    //method not related to this question
    vehicleData bus = getBus(s1);


    Vehicle* arrVehicle[2];
    arrVehicle[0] = new Bus(bus.vehicleID, bus.man, bus.curMileage);


    printf("%i\n", arrVehicle[0]->currentMileage);
    printf("%c\n", arrVehicle[0]->man);

这是我遇到的错误

   ‘class Vehicle’ has no member named ‘currentMileage’

我在互联网上尝试了一些东西,但没有任何效果...错误类似于上述内容。

非常感谢您的回答。

【问题讨论】:

  • 你错过了一个重要的点:每个Bus 都是Vehicle,但不是每个Vehicle 都是Bus,因此并非每辆车都有一个成员currentMileagecurrentMileage 是否应该成为 Vehicle 的一部分?
  • 为了打印数组中的特定数据成员,您需要强制转换为适当的对象,然后访问细节。
  • 你的问题标题会让读者感到困惑。你的问题本质上是关于对象层次结构的,但标题是关于数组的。
  • @churill 不,它不应该是Vehicle 的一部分,它不知道它叫什么,我只需要Bus 对象及其在数组中的所有属性和@987654333 @ 对象,它的所有属性都在同一个数组中。所以我什么都不会错过。似乎每个Bus 只是一个车辆,每个Train 只是一个Vehicle 而不是TrainBus 添加到数组后。这是一个问题,如何将同一父级的不同子级存储在一个数组中。
  • @SergeyA 这太奇怪了。标题讨论了数组中的同父对象。问题是关于数组中的子对象。

标签: c++ arrays oop inheritance


【解决方案1】:

为了实现你想要做的是将Vehicle* 转换为Bus*

vehicleData bus = getBus(s1);

Vehicle* arrVehicle[2];
arrVehicle[0] = new Bus(bus.vehicleID, bus.man, bus.curMileage);

// First method
printf("%i\n", dynamic_cast<Bus*>(arrVehicle[0])->currentMileage);
printf("%c\n", dynamic_cast<Bus*>(arrVehicle[0])->man);

// Second method, don't need to cast everytime
Bus* busInstance = dynamic_cast<Bus*>(arrVehicle[0]);

printf("%i\n", busInstance->currentMileage);
printf("%c\n", busInstance->man);

【讨论】:

    【解决方案2】:

    我过去也有类似的要求,我通过创建一个包含指向不同车辆的指针的双指针数组来达到解决方案:

    //When adding a new vehicle to your array you will need an aux array in order to update the list in a dynamic way
    
    Vehicle **aux = new Vehicle *[vehicle_counter+1]; 
    
    //Then create the new vehicle type with the constructor and it parameters
    
    Bus *_Bus; 
    _Bus= new Bus(vehicle_id,man,currentMileage);
    
    //Finally use this loop to update your vehicle list
    
    for (int i=0;i<vehicle_counter;i++){  list
      aux[i]=list_vehicles[i]; //Adding the current objects in the list to the aux array
    }
    
    //Adding the new vehicle of Bus type (aux has +1 space that the current list)
    
    aux[vehicle_counter] = _Bus; 
    vehicle_counter++;
    list_vehicles=aux; //Updating the list of vehicles with the new vehicle added
    

    这样做,您将能够单独访问不同的对象/参数,并且包含不同车辆(列表)的数组将保持动态。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多