Re: vector assign
Hi!
Dar??o Griffo schrieb:
BTW inheritance is not necessary.
I know, but since we program in OOP paradigm, it seems to me the best
way to do that.
No. It is not the best way although we have "OOP" at our disposal (I
think of "OOP" here as "derive from a base class"). A vector makes no
good base class. You must not derive from it. Value based classes are
usually not suited for inheritance. Think of a "long" derived from an
"int"!?
A better way to do it is to write a complete wrapper and add
functionality as needed:
template<typename T>
class AssignableVector
{
vector<T> data;
public:
AssignableVector() {}
AssignableVector(size_t n, T const t = T())
: data(n, t)
{}
//forward all functions to the vector:
void push_back(T const t) { data.push_back(t); }
...
//add extra functionality:
void assign(/*whatever parameters are needed*/) { ... }
};
This it much code. And it is boring to write. I guess that's why many
people resort to inheritance: just to save typing.
When I can't really convince you not to use inheritance, then at least
use private inheritance to hide it. (Which means you would have to
somehow provide public versions of the inherited methods, which needs
the same typing than above after all).
Regards,
Frank