473,385 Members | 1,813 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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 3262
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

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.