By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,081 Members | 862 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,081 IT Pros & Developers. It's quick & easy.

STL Vectors & Memory

P: n/a
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);
}
Jul 22 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
In article <94**************************@posting.google.com >,
go***********@yahoo.co.uk (Patrick) wrote:
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?
vector's destructor automatically calls the destructors of all of the
contained items, and deallocates the vector's internal buffer.

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();
}


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();
}

Jul 22 '05 #2

P: n/a

"Patrick" <go***********@yahoo.co.uk> wrote in message
news:94**************************@posting.google.c om...
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?
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.

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.
Huh, what gave you that idea?
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.
The elements of the vector cannot be modifed because you are returning a
const_iterator.

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

Yes its called a const_iterator. Somewhere, somehow, you obviously know this
or you wouldn't have chosen to use a const_iterator.
any help appreciated

pat


john
Jul 22 '05 #3

P: n/a
"John Harrison" <jo*************@hotmail.com> wrote in message
news:c1*************@ID-196037.news.uni-berlin.de...

"Patrick" <go***********@yahoo.co.uk> wrote in message
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

Yes its called a const_iterator. Somewhere, somehow, you obviously know

this or you wouldn't have chosen to use a const_iterator.


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
Jul 22 '05 #4

P: n/a
"John Harrison" <jo*************@hotmail.com> wrote in message news:<c1*************@ID-196037.news.uni-berlin.de>...
"Patrick" <go***********@yahoo.co.uk> wrote in message
news:94**************************@posting.google.c om...
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?


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.

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.


Huh, what gave you that idea?
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.


The elements of the vector cannot be modifed because you are returning a
const_iterator.

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


Yes its called a const_iterator. Somewhere, somehow, you obviously know this
or you wouldn't have chosen to use a const_iterator.
any help appreciated

pat


john

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
Jul 22 '05 #5

P: n/a

"Patrick" <go***********@yahoo.co.uk> wrote in message
news:94**************************@posting.google.c om...
"John Harrison" <jo*************@hotmail.com> wrote in message news:<c1*************@ID-196037.news.uni-berlin.de>...
"Patrick" <go***********@yahoo.co.uk> wrote in message
news:94**************************@posting.google.c om...
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?


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.

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.


Huh, what gave you that idea?
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.


The elements of the vector cannot be modifed because you are returning a
const_iterator.

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


Yes its called a const_iterator. Somewhere, somehow, you obviously know this or you wouldn't have chosen to use a const_iterator.
any help appreciated

pat


john

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?


An iterator can 'point to' any element of a container, or
one past the last one.
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.
Get this book:
www.josuttis.com/libbook
I found that it paid for itself in a single day.
As for the const_iterator, maybe i am wrong, but from what i read
May I ask what you're reading?
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.
No. A 'const_iterator' disallows modification of what it points to.
Similar to a pointer to const, e.g. 'const int *'

I understood that a const_iterator does not prevent a client from
altering objects contained withing the container.
You understand incorrectly. :-)

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
on the vector's *elements*. (That is if you use a const_iterator)
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?


You've got it exactly backwards. :-)

-Mike
Jul 22 '05 #6

P: n/a
>

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.


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
Jul 22 '05 #7

P: n/a
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..
Jul 22 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.