Ship *ship;
if (newShipType == 0)
{
ship = new SmallShip(gridPosition.x, gridPosition.y,
grid->raw);
}
else if (newShipType == 1)
{
ship = new MediumShip(gridPosition.x, gridPosition.y,
grid->raw);
}
else // 2
{
ship = new BigShip(gridPosition.x, gridPosition.y,
grid->raw);
}
我有我想要简化的代码:
Ship *ship = new getShipByType[newShipType](gridPosition.x, gridPosition.y, grid->raw);
这样的事情可能吗?
Ship getShipByType[3] = {SmallShip, MediumShip, BigShip};
这给出了:
error: expected primary-expression before ‘,’ token
error: expected primary-expression before ‘,’ token
error: expected primary-expression before ‘}’ token
我真的没想到它会编译,只是在寻找一种更简单的方法来做这件事,这只是一个很长的尝试。