473,465 Members | 1,946 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Vector

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);
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.

Is there any common approach for this? What do you hint me?

Thanks in advance

Ernesto

Oct 7 '06 #1
2 3269
ernesto wrote:
Hi:

I want to create my own vector class;
Why? Many of the answers to the questions below will depend on your reasons
and design goals for the Vector class.

Generally: do not roll your own Vector class: std::vector<is there for a
reason. I can only think of two valid reasons to roll your own
std::vector<replacement:

a) You want to learn how it's done.
b) You need to use vector<Tfor an incomplete type T or a type that for
some other reasons does not fullfill the requirements of std::vector<>
(e.g., T might be copy-constructible but not assignable). Then, you need an
implementation that makes stronger niceness guarantees than the standard
requires or works around such limitations in some other way.
I want to provide methods like:

class Vector
{
public:
void add(const Object* aVal);
call that push_back().
void remove(const Object* aVal);
don't try that. Is this supposed to remove all elements of a given value? Do
you really want to remove by value or do you want to remove at a certain
position. Are you sure you are designing a vector? Could it be that you are
thinking about implementing a std::set<replacement?
};

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);
Yes.
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;
Yes.

using pointer references I should take care about the memory management of
my objects.
Well, at least you need to be clear about who the owner is.

Is there any common approach for this?
Yes: use std::vector.

If you really need your own, try to mimmick std::vector as closely as
possible. In this case: take the arguments by const reference and use the
copy constructor to copy them.
Best

Kai-Uwe Bux
Oct 7 '06 #2

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.

Oct 7 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: {AGUT2}=IWIK= | last post by:
Hello all, It's my fisrt post here and I am feeling a little stupid here, so go easy.. :) (Oh, and I've spent _hours_ searching...) I am desperately trying to read in an ASCII...
9
by: luigi | last post by:
Hi, I am trying to speed up the perfomance of stl vector by allocating/deallocating blocks of memory manually. one version of the code crashes when I try to free the memory. The other version...
7
by: Forecast | last post by:
I run the following code in UNIX compiled by g++ 3.3.2 successfully. : // proj2.cc: returns a dynamic vector and prints out at main~~ : // : #include <iostream> : #include <vector> : : using...
34
by: Adam Hartshorne | last post by:
Hi All, I have the following problem, and I would be extremely grateful if somebody would be kind enough to suggest an efficient solution to it. I create an instance of a Class A, and...
10
by: Bob | last post by:
Here's what I have: void miniVector<T>::insertOrder(miniVector<T>& v,const T& item) { int i, j; T target; vSize += 1; T newVector; newVector=new T;
8
by: Ross A. Finlayson | last post by:
I'm trying to write some C code, but I want to use C++'s std::vector. Indeed, if the code is compiled as C++, I want the container to actually be std::vector, in this case of a collection of value...
16
by: Martin Jørgensen | last post by:
Hi, I get this using g++: main.cpp:9: error: new types may not be defined in a return type main.cpp:9: note: (perhaps a semicolon is missing after the definition of 'vector') main.cpp:9:...
23
by: Sanjay Kumar | last post by:
Folks, I am getting back into C++ after a long time and I have this simple question: How do pyou ass a STL container like say a vector or a map (to and from a function) ? function: ...
6
by: zl2k | last post by:
hi, there I am using a big, sparse binary array (size of 256^3). The size may be changed in run time. I first thought about using the bitset but found its size is unchangeable. If I use the...
24
by: toton | last post by:
Hi, I want to have a vector like class with some additional functionality (cosmetic one). So can I inherit a vector class to add the addition function like, CorresVector : public...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.