How to return a 'read-only' copy of a vector

Posted by michael on Stack Overflow See other posts from Stack Overflow or by michael
Published on 2010-03-17T20:44:37Z Indexed on 2010/03/17 20:51 UTC
Read the original article Hit count: 182

Filed under:

Hi,

I have a class which has a private attribute vector rectVec;

class A {
private:
   vector<Rect> rectVec;
};

My question is how can I return a 'read-only' copy of my Vector? I am thinking of doing this:

class A {
public:
  const vect<Rect>& getRectVec() { return rectVect; }
}

Is that the right way? I am thinking this can guard against the callee modify the vector(add/delete Rect in vector), what about the Rect inside the vector?

© Stack Overflow or respective owner

Related posts about c++