使用std::vector
:
#include <vector>
class vectorOfInt
{
public:
private:
int _size; // probably you want to remove this, use _vector.size() instead.
std::vector<int> _vector;
};
vectorOfInt::vectorOfInt()
:_size(32),
_vector(size)
{
}
编辑:由于您不想使用std::vector
,您必须自己处理内存。如果您在编译时知道数组的大小,则可以使用内置数组,但我怀疑情况是否如此。您必须执行以下操作:
#include <memory>
class vectorOfInt
{
public:
private:
int _size;
// If C++11 is not an option, use a raw pointer.
std::unique_ptr<int[]> _vector;
};
vectorOfInt::vectorOfInt()
:_size(32),
_vector(new int[size])
{
}