我有實例的數組,在我的代碼,像這樣:如何計算實例數組中未刪除的實例?
class Squad : public ISquad
{
public:
Squad(void);
Squad(Squad const & src);
virtual ~Squad(void);
int getCount(void) const;
int push(ISpaceMarine*);
ISpaceMarine* getUnit(int) const;
ISpaceMarine** getUnits(void) const;
int getCapacity(void) const;
Squad & operator=(Squad const & rhs);
private:
ISpaceMarine **_units; // HERE IS THE ARRAY OF INSTANCES
int const _squadCapacity;
};
初始化像這樣在構造
Squad::Squad(void) : _units(new ISpaceMarine*[64]), _squadCapacity(64)
{
return;
}
首先,是的好辦法嗎?
如果是,我嘗試計算數組中有效實例的數量(不是NULL
,而不是deleted
),但我不知道如何檢查_units[20]
是否被刪除。
我該怎麼辦?
他是我目前做的方式:
int Squad::getCount(void) const
{
int count = 0;
while (this->_units[count] != NULL && count < this->_squadCapacity)
count++;
return count;
}
的可能性? 'std :: list'似乎是合適的。 – JHBonarius
您無法檢查指針的目標是否被刪除。刪除後直接將其設置爲NULL即可。或者從列表中刪除它。或使用智能指針。 – flyx
您不能檢查指針是否有效。不要使用原始指針。 –