C
cppaddict
I'm confused about const references. I thought they provided a way to
give access to private members without allowing those members to be
changed. However, the following client code successfully deletes the
referred to member vector:
PointCollection pc;
pc.addSomePoints();
std::vector<Point> mem = pc.getPoints(); //returns a const ref
cout << mem.size() << endl; //outputs 3, or whatever # of
points happens to be
mem.clear();
cout << mem.size() << endl; //outputs 0!!
Here is the the class in question:
class PointCollection {
private:
std::vector<int> m_points;
public:
const std::vector<Point>& PointCollection::getPoints()
{
return m_points;
}
//other methods to add points, etc, left out for
brevity
}
Thanks for any help,
cpp
give access to private members without allowing those members to be
changed. However, the following client code successfully deletes the
referred to member vector:
PointCollection pc;
pc.addSomePoints();
std::vector<Point> mem = pc.getPoints(); //returns a const ref
cout << mem.size() << endl; //outputs 3, or whatever # of
points happens to be
mem.clear();
cout << mem.size() << endl; //outputs 0!!
Here is the the class in question:
class PointCollection {
private:
std::vector<int> m_points;
public:
const std::vector<Point>& PointCollection::getPoints()
{
return m_points;
}
//other methods to add points, etc, left out for
brevity
}
Thanks for any help,
cpp