nat*_*928 2 c++ polymorphism inheritance dynamic
我有一个主要的抽象类,我必须创建基于这个类的子类(这不能被修改):
class Spaceship
{
protected:
string m_name; // the name of the ship
int m_hull; // the hull strenght
public:
// Purpose: Default Constructor
// Postconditions: name and hull strength set to parameters
// -- INLINE
Spaceship(string n, int h)
{
m_name = n;
m_hull = h;
}
// Purpose: Tells if a ship is alive.
// Postconditions: 'true' if a ship's hull strength is above zero,
// 'false' otherwize.
// -- INLINE
bool isAlive()
{
return (m_hull > 0);
}
// Purpose: Prints the status of a ship.
// -- VIRTUAL
virtual void status() const = 0;
// Purpose: Changes the status of a ship, when hit by a
// weapon 's' with power level 'power'
// -- VIRTUAL
virtual void hit(weapon s, int power) = 0;
string getName() const
{
return m_name;
}
}; //Spaceship
Run Code Online (Sandbox Code Playgroud)
这是我的子课程的一个例子:
class Dreadnought: public Spaceship
{
int m_shield;
int m_armor;
int m_type;
public:
Dreadnought( string n, int h, int a, int s ): Spaceship( n, h ),m_shield( s ),m_armor(a),m_type(dreadnought){}
virtual void status() const
{
// implementation not shown to save space
}
virtual void hit(weapon s, int power)
{
// implementation not shown to save space
}
int typeOf(){ return m_type; }
};
Run Code Online (Sandbox Code Playgroud)
在我的主代码中,我有一个不同类型的太空飞船的动态数组:
Spaceship ** ships;
cin >> numShips;
// create an array of the ships to test
ships = new Spaceship * [numShips];
Run Code Online (Sandbox Code Playgroud)
然后我从用户那里得到输入,以在这个数组中声明不同类型的船只:
ships[0] = new Dreadnought( name, hull, armor, shield );
Run Code Online (Sandbox Code Playgroud)
我的问题是,当我去删除数组时,没有调用正确的析构函数,而是调用了Spaceships,这是否会产生内存泄漏,因为成员变量"m_shield,m_armor"没有被删除并且被挂起?如果是这样,有一种更好的方法来获取类型而不是使用var m_type并调用:
if( ships[i]->typeOf() == 0 )
delete dynamic_cast<Frigate*>(ships[i]);
else if( ships[i]->typeOf() == 1 )
delete dynamic_cast<Destroyer*>(ships[i]);
else if( ships[i]->typeOf() == 2 )
delete dynamic_cast<Battlecruiser*>(ships[i]);
else if( ships[i]->typeOf() == 3 )
delete dynamic_cast<Dreadnought*>(ships[i]);
else
delete dynamic_cast<Dropship*>(ships[i]);
Run Code Online (Sandbox Code Playgroud)
我声明的Spaceship类中的问题#2:virtual int typeOf()= 0; 并评论出来,有没有办法我可以在子类中实现这个函数而不在父类中声明,所以我可以像上面显示的那样使用它?当我没有声明它我得到编译器错误:
错误:'class Spaceship'没有名为'typeOf'的成员
我认为这又与动态套管有关.
任何帮助都会很棒,
谢谢nat
编辑:
如果我刚刚做了以下事情,那么我的第一个问题就会被记忆泄漏:
删除船舶[i];
或者我应该这样做:
delete dynamic_cast(ships [i]);
删除仅在派生类中的成员变量?
Thnaks
您必须向Spaceship类中添加虚拟析构函数.然后delete将正确地销毁数组中的元素.
您必须在中声明typeOf()方法Spaceship.否则,编译器无法知道,这是一个有效的成员函数.
typeOf()如果在基类中添加所需的功能作为虚拟成员函数,则可以避免.
即使您出于某种原因无法修改基类,也可以执行dynamic_cast并测试,如果它产生空指针
Frigate *p = dynamic_cast<Frigate*>(ships[i]);
if (p != 0) {
// do something with a frigate
}
Run Code Online (Sandbox Code Playgroud)