Re: Question on Constant Variables
Hi all,
I guess I didnt ask the question properly. Let me try again. Im
writing a vector class that wraps the STL vector container. Im
considering the situation as follows: Lets say a have a vector Y of 3
elements. I want to assign it to elements 1 to 3 in a vector X. Then i
do X(1,3) = Y. So in this case, the operator overload (const int& i,
const int& j) for object X gets called first, and creates (im not sure
if this is the right name) an iterator class object VectorItr<TYPE>
(lets call it Z), and passing the principle argument of X along. Then
in (b), the consturctor for Z uses a member function of X to access
the begin interator of v, which is private in X. It sets vitr to the
begin iterator of v, and exits the constructor. The overload operator
= gets called next, and calls the function assign in question. Assign
is marked in (c). Then my original question follows:
To my knowledge, this means that all private variables in this
object
cannot be changed. however, the iterator to the private variable v is
not constant.
Can somebody explain to me why I still can change stuff in the
private variable v??
Thanks again, sorry for the first time.
//********************************************************
// VectorIterators Class
// this is a class to manage iterators of the Vector Class
template <typename TYPE>
class VectorItr {
typename vector<TYPE>::iterator vitr;
int n;
public:
//assignments
VectorItr<TYPE>& operator=(const Vector<TYPE>& rhs) {
//using the iterator, populate the Vector<TYPE>
rhs.assign(vitr,n,rhs);
return *this; //should i return this?
}
//********************************************************
// Constructors and Destructor
VectorItr() { };
~VectorItr(){ };
VectorItr( Vector<TYPE>& val, const int& i, const int& j) { <------
(b)
//this constructor is used to initialize the beginning of the
//iterator, and the number of elements that need to be assigned
vitr = val.begin()+i; n = j-i+1;
}
};
//********************************************************
// Main Vector Class
template <typename TYPE>
class Vector {
vector<TYPE> v;
public:
VectorItr<TYPE> operator()(const int& i,const int& j)
<-------- (a)
{
return VectorItr<TYPE> (*this, i, j);
}
//********************************************************
// Constructors and Destructor
Vector() { };
~Vector(){ };
//********************************************************
// vector container functions
void assign(typename vector<TYPE>::iterator itr, const int& n,
const
Vector<TYPE>& val) const { <----- (c)
for (int k=0; k<n; k++){
*itr = val[k]; itr++;
}
}
};