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