Zakładając, że przez "funkcję inicjalizującą" masz na myśli konstruktor oraz masz używać starego C++:
class Container
{
public:
typedef int ValueType;
~Container() {
delete values;
}
Container(int capacity)
: mCapacity(capacity)
, mSize(0)
, mValues(new ValueType[capacity])
{
}
Container(const Container& other)
: mCapacity(other.size())
, mSize(other.size())
, mValues(new ValueType[mCapacity])
{
std::copy(other.mValues, other.mValues + mSize, mValues);
}
Container& operator=(const Container &other)
{
if (capacity() < other.size()) {
reserve(other.size());
}
mSize = other.size();
std::copy(other.mValues, other.mValues + size(), mValues);
}
ValueType at(size_t i) const {
if (i >= mSize) throw std::invalid_argument("Index out of range");
return mValues[i];
}
void append(ValueType x)
{
if (mSize == mCapacity) reserve(3 * mSize / 2);
mValues[mSize ++] = x;
}
size_t size() const { return mSize; }
size_t capacity() const { return mCapacity; }
private:
void reserve(size_t) {
throw std::logic_error("Capacity update not implemented");
}
size_t mCapacity;
size_t mSize;
ValueType* mValues;
};
Przy czym jest to upośledzona wersja std::vector