我很难理解为什么engineNum
从类的函数中无法访问该变量Pickup
。我的基本理解是,如果类被继承,私有变量应该是可访问的。这不是我发现的情况:
class Truck
{
private:
string model;
Truck() {};
static int TruckEngineNum;
int engineNum;
public:
Truck(const string& model)
{
this->model = model;
engineNum = TruckEngineNum++;
};
string getModel()
{
return model;
}
int getEngineNum()
{
return engineNum;
}
};
int Truck::TruckEngineNum = 100;
class Pickup : public Truck
{
public:
Pickup(const string& model) : Truck(model)
{
if((engineNum % 2) == 1){ engineNum++; };
}
};