STL Vectors & Memory

P

Patrick

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

Clark Cox

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

John Harrison

Patrick said:
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
 
M

Mike Wahler

John Harrison said:
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
 
P

Patrick

John Harrison said:
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.


Huh, what gave you that idea?


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


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


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
 
M

Mike Wahler

Patrick said:
"John Harrison" <[email protected]> wrote in message


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
 
J

John Harrison

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
 
P

Patrick

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,008
Latest member
Rahul737

Latest Threads

Top