473,769 Members | 2,365 Online
Bytes | Software Development & Data Engineering Community
+ 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 3300
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<rep lacement:

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<replac ement?
};

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 <vectorinclud ed 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
3207
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 "stereolithography" file (*.STL) into my program. This has the following syntax... Begin STL Snippet **********
9
2974
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 seem to work. I would appreciate someone to comment on this. Version 1 (crashes on deallocating) #include <iostream>
7
10629
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 namespace std; : : vector<string>* getTyphoon()
34
4176
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 "push_back" a copy of this into a vector V. This is repeated many times in an iterative process. Ok whenever I "push_back" a copy of Class A, I also want to assign a pointer contained in an exisiting instance of a Class B to this
10
4843
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
5114
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 types or std::vector<int>. So where I would use an int* and reallocate it from time to time in C, and randomly access it via , then I figure to copy the capacity and reserve methods, because I just need a growable array. I get to considering...
16
4440
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: error: two or more data types in declaration of 'set' I don't really see the problem... Here's the code:
23
6545
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: vector<string> tokenize(string s){
6
6830
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 vector<bool>, does each element takes 4 bytes instead of 1 bit? I am using gcc3.4.4. There is a bit_vector which is kind of old so I wont use that. Any other choices? Thanks ahead. zl2k
24
2960
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 vector<Corres>{ public: void addCorres(Corres& c); //it do little more than push_back function. }
0
9586
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
1
9990
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9861
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8869
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7406
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6672
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
3956
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 we have to send another system
2
3561
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2814
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.