【发布时间】:2016-02-11 05:45:19
【问题描述】:
所以我已经为这个任务苦苦挣扎了一段时间。我修复了我所有的记忆问题(几天的逻辑),现在我被困在似乎最简单的事情上。我必须在两个数组已满时扩展它们,这会扩展它们,但不会复制数据,关于从这里去哪里有什么建议吗?
List::List(const List& x, int b)
{
if(b == 1) // if car needs to grow
{
size1 = x.size1 * 2;
car = new Car[size1];
for(int i = 0; i < count1; i++)
car[i] = x.car[i]; // what copies .. I think...
}
else if(b == 2) // if motorcycle array needs to grow
{
size2 = x.size2 * 2;
motor = new Motorcycle[size2];
for(int i = 0; i < count2; i++)
{
motor[i] = x.motor[i]; // what copies .. I think...
}
}
}
我在这两个类中也有一个赋值运算符重载
Car& Car::operator=( Car x)
{
this->setModel(x.rModel());
this->setMake(x.rMake());
this->setPrice(x.Vehicle::getPrice());
this->setMileage(x.rMiles());
this->setLot(x.rLot());
this->setGas(x.rGas());
this->seats = x.rSeats();
this->luxury = x.rLux();
}
摩托车也是如此
Motorcycle& Motorcycle::operator=( Motorcycle x)
{
this->setModel(x.rModel());
this->setMake(x.rMake());
this->setPrice(x.Vehicle::getPrice());
this->setMileage(x.rMiles());
this->setLot(x.rLot());
this->setGas(x.rGas());
this->style = x.rStyle();
this->passenger = x.rPassenger();
}
如果你帮帮我,我会把我拔掉的头发发给你。
完整代码:https://drive.google.com/folderview?id=0B7LaFMkHcgCAd3habUpjWUR0YU0&usp=sharing
【问题讨论】:
-
为什么不使用
std::vector<Car> cars? -
Proffesor 正在教我们向量的后端
-
你如何确定元素没有被复制?显示使用该类的代码。
标签: c++ operator-overloading copy-constructor inventory