Connecting Tech Pros Worldwide Help | Site Map

STL Vectors & Memory

 
LinkBack Thread Tools Search this Thread
  #1  
Old July 22nd, 2005, 06:47 AM
Patrick
Guest
 
Posts: n/a
Default STL Vectors & Memory

In class *ClassA* below I have an STL Vector *vec* as a member
variable of the class.
Do I have to create a destructer, and somehow deallocate the memory
from *vec*, or is this handled automatically?

Also I want to allow a client of *ClassA* to be able to view the
elements of the vector.
Returning a reference to *vec* would be bad OO programming as i would
be returning a reference to a private member of the class.
I was thinking of using a method that would return an iterator over
the Vector i.e.

vector<int>::const_iterator ClassA::getIterator()
{ return vec.begin();
}

However i realised then that STL iterators dont have a reference to
the last element in the container, so this isnt viable. Also this is
pretty much the bad OO programming in the sense that while its a const
iterator and the Vector itself cant be modified, the elements of the
vector can be modified, i.e. once again allowing a client to modify
private data of the class.

Is there a design pattern or some construct of allowing the client to
view elements of the vector in a "read-only" fashion, while not
violating OO principles?...other than copying the entire vector

any help appreciated

pat



------------------- ClassA.h -----------------------------
#ifndef CLASSA_H
#define CLASSA_H

#include <vector>

class ClassA
{ public:
ClassA();
void addElement(int element);

private:
vector<int> vec;
};

#endif
------------------ ClassA.cpp ----------------------------
#include <vector>
using namespace std;

#include "ClassA.h"

ClassA::ClassA(){}

void ClassA::addElement(int element)
{ vec.push_back(element);
}

  #2  
Old July 22nd, 2005, 06:47 AM
Clark Cox
Guest
 
Posts: n/a
Default Re: STL Vectors & Memory

In article <94740f05.0402230919.4c2ee747@posting.google.com >,
googleaddress@yahoo.co.uk (Patrick) wrote:
[color=blue]
> In class *ClassA* below I have an STL Vector *vec* as a member
> variable of the class.
> Do I have to create a destructer, and somehow deallocate the memory
> from *vec*, or is this handled automatically?[/color]

vector's destructor automatically calls the destructors of all of the
contained items, and deallocates the vector's internal buffer.
[color=blue]
>
> Also I want to allow a client of *ClassA* to be able to view the
> elements of the vector.
> Returning a reference to *vec* would be bad OO programming as i would
> be returning a reference to a private member of the class.
> I was thinking of using a method that would return an iterator over
> the Vector i.e.
>
> vector<int>::const_iterator ClassA::getIterator()
> { return vec.begin();
> }[/color]

Why not implement two functions:

vector<int>::const_iterator ClassA::begin() const
{
return vec.begin();
}

vector<int>::const_iterator ClassA::end() const
{
return vec.end();
}

  #3  
Old July 22nd, 2005, 06:48 AM
John Harrison
Guest
 
Posts: n/a
Default Re: STL Vectors & Memory


"Patrick" <googleaddress@yahoo.co.uk> wrote in message
news:94740f05.0402230919.4c2ee747@posting.google.c om...[color=blue]
> In class *ClassA* below I have an STL Vector *vec* as a member
> variable of the class.
> Do I have to create a destructer, and somehow deallocate the memory
> from *vec*, or is this handled automatically?[/color]

It's handled automatically. Have confidence in C++, if it really was the way
you are worried about then that would a total nightmare and I would give up
C++ and start programming Java.
[color=blue]
>
> Also I want to allow a client of *ClassA* to be able to view the
> elements of the vector.
> Returning a reference to *vec* would be bad OO programming as i would
> be returning a reference to a private member of the class.
> I was thinking of using a method that would return an iterator over
> the Vector i.e.
>
> vector<int>::const_iterator ClassA::getIterator()
> { return vec.begin();
> }
>
> However i realised then that STL iterators dont have a reference to
> the last element in the container, so this isnt viable.[/color]

Huh, what gave you that idea?
[color=blue]
> Also this is
> pretty much the bad OO programming in the sense that while its a const
> iterator and the Vector itself cant be modified, the elements of the
> vector can be modified, i.e. once again allowing a client to modify
> private data of the class.[/color]

The elements of the vector cannot be modifed because you are returning a
const_iterator.
[color=blue]
>
> Is there a design pattern or some construct of allowing the client to
> view elements of the vector in a "read-only" fashion, while not
> violating OO principles?...other than copying the entire vector
>[/color]

Yes its called a const_iterator. Somewhere, somehow, you obviously know this
or you wouldn't have chosen to use a const_iterator.
[color=blue]
> any help appreciated
>
> pat
>[/color]

john


  #4  
Old July 22nd, 2005, 06:48 AM
Mike Wahler
Guest
 
Posts: n/a
Default Re: STL Vectors & Memory

"John Harrison" <john_andronicus@hotmail.com> wrote in message
news:c1dj9o$1hkqvk$1@ID-196037.news.uni-berlin.de...[color=blue]
>
> "Patrick" <googleaddress@yahoo.co.uk> wrote in message[color=green]
> > Is there a design pattern or some construct of allowing the client to
> > view elements of the vector in a "read-only" fashion, while not
> > violating OO principles?...other than copying the entire vector
> >[/color]
>
> Yes its called a const_iterator. Somewhere, somehow, you obviously know[/color]
this[color=blue]
> or you wouldn't have chosen to use a const_iterator.[/color]

I don't know if it's the case here, but I find that many folks copy
and use code snippets without really knowing what they mean.

-Mike


  #5  
Old July 22nd, 2005, 06:48 AM
Patrick
Guest
 
Posts: n/a
Default Re: STL Vectors & Memory

"John Harrison" <john_andronicus@hotmail.com> wrote in message news:<c1dj9o$1hkqvk$1@ID-196037.news.uni-berlin.de>...[color=blue]
> "Patrick" <googleaddress@yahoo.co.uk> wrote in message
> news:94740f05.0402230919.4c2ee747@posting.google.c om...[color=green]
> > In class *ClassA* below I have an STL Vector *vec* as a member
> > variable of the class.
> > Do I have to create a destructer, and somehow deallocate the memory
> > from *vec*, or is this handled automatically?[/color]
>
> It's handled automatically. Have confidence in C++, if it really was the way
> you are worried about then that would a total nightmare and I would give up
> C++ and start programming Java.
>[color=green]
> >
> > Also I want to allow a client of *ClassA* to be able to view the
> > elements of the vector.
> > Returning a reference to *vec* would be bad OO programming as i would
> > be returning a reference to a private member of the class.
> > I was thinking of using a method that would return an iterator over
> > the Vector i.e.
> >
> > vector<int>::const_iterator ClassA::getIterator()
> > { return vec.begin();
> > }
> >
> > However i realised then that STL iterators dont have a reference to
> > the last element in the container, so this isnt viable.[/color]
>
> Huh, what gave you that idea?
>[color=green]
> > Also this is
> > pretty much the bad OO programming in the sense that while its a const
> > iterator and the Vector itself cant be modified, the elements of the
> > vector can be modified, i.e. once again allowing a client to modify
> > private data of the class.[/color]
>
> The elements of the vector cannot be modifed because you are returning a
> const_iterator.
>[color=green]
> >
> > Is there a design pattern or some construct of allowing the client to
> > view elements of the vector in a "read-only" fashion, while not
> > violating OO principles?...other than copying the entire vector
> >[/color]
>
> Yes its called a const_iterator. Somewhere, somehow, you obviously know this
> or you wouldn't have chosen to use a const_iterator.
>[color=green]
> > any help appreciated
> >
> > pat
> >[/color]
>
> john[/color]


Thanks for the replies, im new to c++ hence my confusion.

John you seemed to be hinting that Iterators in C++ do contain a
reference to the last element in the container, is this so? I have
been searching for info on this but all the examples i have seen use
the container to explicitly get the reference to the last element.

As for the const_iterator, maybe i am wrong, but from what i read i
understood that a cont_iterator only prevented one from carrying out
operations on the container itself e.g. adding elements to the vector,
removing elements from the vector.

I understood that a const_iterator does not prevent a client from
altering objects contained withing the container.

So if the vector contained within my *ClassA* contains objects of type
*ClassB*, when i return an iterator to a client of *ClassA* it will
prevent the client from carrying out (destructive) operations on the
vector composed within *ClassA*, however it will not prevent the
client from carrying out destructive operations on the objects of type
*ClassB* that are contained within the vector. Is this not true?

pat
  #6  
Old July 22nd, 2005, 06:48 AM
Mike Wahler
Guest
 
Posts: n/a
Default Re: STL Vectors & Memory


"Patrick" <googleaddress@yahoo.co.uk> wrote in message
news:94740f05.0402231635.5b02644e@posting.google.c om...[color=blue]
> "John Harrison" <john_andronicus@hotmail.com> wrote in message[/color]
news:<c1dj9o$1hkqvk$1@ID-196037.news.uni-berlin.de>...[color=blue][color=green]
> > "Patrick" <googleaddress@yahoo.co.uk> wrote in message
> > news:94740f05.0402230919.4c2ee747@posting.google.c om...[color=darkred]
> > > In class *ClassA* below I have an STL Vector *vec* as a member
> > > variable of the class.
> > > Do I have to create a destructer, and somehow deallocate the memory
> > > from *vec*, or is this handled automatically?[/color]
> >
> > It's handled automatically. Have confidence in C++, if it really was the[/color][/color]
way[color=blue][color=green]
> > you are worried about then that would a total nightmare and I would give[/color][/color]
up[color=blue][color=green]
> > C++ and start programming Java.
> >[color=darkred]
> > >
> > > Also I want to allow a client of *ClassA* to be able to view the
> > > elements of the vector.
> > > Returning a reference to *vec* would be bad OO programming as i would
> > > be returning a reference to a private member of the class.
> > > I was thinking of using a method that would return an iterator over
> > > the Vector i.e.
> > >
> > > vector<int>::const_iterator ClassA::getIterator()
> > > { return vec.begin();
> > > }
> > >
> > > However i realised then that STL iterators dont have a reference to
> > > the last element in the container, so this isnt viable.[/color]
> >
> > Huh, what gave you that idea?
> >[color=darkred]
> > > Also this is
> > > pretty much the bad OO programming in the sense that while its a const
> > > iterator and the Vector itself cant be modified, the elements of the
> > > vector can be modified, i.e. once again allowing a client to modify
> > > private data of the class.[/color]
> >
> > The elements of the vector cannot be modifed because you are returning a
> > const_iterator.
> >[color=darkred]
> > >
> > > Is there a design pattern or some construct of allowing the client to
> > > view elements of the vector in a "read-only" fashion, while not
> > > violating OO principles?...other than copying the entire vector
> > >[/color]
> >
> > Yes its called a const_iterator. Somewhere, somehow, you obviously know[/color][/color]
this[color=blue][color=green]
> > or you wouldn't have chosen to use a const_iterator.
> >[color=darkred]
> > > any help appreciated
> > >
> > > pat
> > >[/color]
> >
> > john[/color]
>
>
> Thanks for the replies, im new to c++ hence my confusion.
>
> John you seemed to be hinting that Iterators in C++ do contain a
> reference to the last element in the container, is this so?[/color]

An iterator can 'point to' any element of a container, or
one past the last one.
[color=blue]
> I have
> been searching for info on this but all the examples i have seen use
> the container to explicitly get the reference to the last element.[/color]

Get this book:
www.josuttis.com/libbook
I found that it paid for itself in a single day.
[color=blue]
> As for the const_iterator, maybe i am wrong, but from what i read[/color]

May I ask what you're reading?
[color=blue]
> i
> understood that a cont_iterator only prevented one from carrying out
> operations on the container itself e.g. adding elements to the vector,
> removing elements from the vector.[/color]

No. A 'const_iterator' disallows modification of what it points to.
Similar to a pointer to const, e.g. 'const int *'
[color=blue]
>
> I understood that a const_iterator does not prevent a client from
> altering objects contained withing the container.[/color]

You understand incorrectly. :-)
[color=blue]
>
> So if the vector contained within my *ClassA* contains objects of type
> *ClassB*, when i return an iterator to a client of *ClassA* it will
> prevent the client from carrying out (destructive) operations on the
> vector[/color]

on the vector's *elements*. (That is if you use a const_iterator)
[color=blue]
>composed within *ClassA*, however it will not prevent the
> client from carrying out destructive operations on the objects of type
> *ClassB* that are contained within the vector. Is this not true?[/color]

You've got it exactly backwards. :-)

-Mike


  #7  
Old July 22nd, 2005, 06:49 AM
John Harrison
Guest
 
Posts: n/a
Default Re: STL Vectors & Memory

>[color=blue]
>
> Thanks for the replies, im new to c++ hence my confusion.
>
> John you seemed to be hinting that Iterators in C++ do contain a
> reference to the last element in the container, is this so? I have
> been searching for info on this but all the examples i have seen use
> the container to explicitly get the reference to the last element.
>[/color]

OK, I get your problem. It's true that given an iterator only, you cannot
derive iterator which points to the last element of a container. But its
equally true that given an iterator you cannot derive the iterator which
points to the first element of a containers either. And both are true of
pointers and arrays upon which iterators are modelled.

Normally what is done is to define two methods on your container

vector<int>::const_iterator ClassA::getBegin() const
{
return vec.begin();
}


vector<int>::const_iterator ClassA::getEnd() const
{
return vec.end();
}

And then have the rest of your code operate on a range of iterators, i.e.
from begin up to (but not including) end.

I'm guess that you are thinking of Java where iterators have a next method
which returns true or false. You can of course code it like that yourself,
but it would mean writing your own iterator class, and it wouldn't be the
C++ way so it would confuse.

john


  #8  
Old July 22nd, 2005, 06:49 AM
Patrick
Guest
 
Posts: n/a
Default Re: STL Vectors & Memory

I have seen the light!

So to summarise, a const_iterator prevents a client from modifying the
elements of the collection\container right?

thanks all..
 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,989 network members.