ernesto wrote:
Hi:
I want to create my own vector class; I want to provide methods like:
class Vector
{
public:
void add(const Object* aVal);
void remove(const Object* aVal);
};
etc.
I want to use good C++ design guidelines, and in C++, the people is
encouraged to use references instead of pointers, so, my methods
signatures would be:
void add(const Object& aVal);
void remove(const Object& aVal);
Then base your container on the std::vector. It will make you learn how
the STL container and its interface works.
#include <vector>
template< typename T >
class Vector
{
std::vector< T vt;
public:
Vector() vt() { }
~Vector() { }
void push_back(T& t) { vt.push_back(t); }
size_t size() const { return vt.size() }
.... etc
};
int main()
{
Vector< int vn;
vn.push_back(11);
}
and expand the class whenever you need another of std::vector's
features. Thats a lot of carefull work. You'll need a copy ctor,
assignment operator, clear(), operator[] and then iterators. Whats cool
is that you have a header to consult, the <vectorincluded above.
You can overload the global op<< to be able to stream all the Vector's
elements in one line.
By the time you start realizing how usefull doing this can be, you'll
end up using that Vector class of yours repeatedly in real projects.
>
Using references, I should use copy constructors for store data in my
vector (because the references do not give me information about the
object lifetime) and all that stuff; using pointer references I should
take care about the memory management of my objects.
Let the member std::vector worry about that. Instead: learn the
difference between a std::deque and a std::vector. The point here is
that there are different type of iterators and different ways to store
elements. Each different container has a specific reason and purpose.
Some are efficient at doing something and inefficient at doing that.
Some use forward and/or reverse iterators, others have bidirectional or
random iterators. etc.
Once you've got the std::vector under the belt, other containers will
look the same except for their unique features. push_back(), pop() and
size() are common member functions of many containers.
>
Is there any common approach for this? What do you hint me?
don't reinvent the wheel, use the std::vector, you'll not find a better
design for its purpose. Its rock solid.