[英]modifying an object's std::vector from within another class
In my program, a group
object has an std::vector
full of polygons (a ring and holes) and a polygon
object has an std::vector
of points. 在我的程序中, group
对象的std::vector
充满了多边形(一个圆环和孔),而polygon
对象的点则是std::vector
。 In reality, the classes are more complicated, I've stripped them down here to illustrate my problem. 实际上,这些类更为复杂,我在这里将它们简化了下来以说明我的问题。
I want to be able to modify the point
'sx and y coordinates from within it's corresponding group object. 我希望能够从其相应的组对象中修改point
'sx和y坐标。 Below, in group.cpp
, I have a dummy function called void Group::tryChangingAllX()
that tries to accomplish this. 下面,在group.cpp
,我有一个名为void Group::tryChangingAllX()
的虚拟函数,试图完成此任务。 However, calling show()
on the group afterwards shows no change to it's polygon's point's coordinates. 但是,随后在组上调用show()
并不会改变其多边形点的坐标。
I think I need to use references/pointers, but I need a nudge in the right direction. 我想我需要使用引用/指针,但是我需要朝着正确的方向轻推。
#include "point.h"
#include <iostream>
Point::~Point(){}
Point::Point(int x, int y){
_x = x;
_y = y;
}
void Point::show(){std::cout << "(" << x() << "," << y() << ")";}
void Point::x(int x){_x = x;}
void Point::y(int y){_y = y;}
int Point::x(){return _x;}
int Point::y(){return _y;}
#ifndef POINT_GUARD
#define POINT_GUARD
class Point{
int _x;
int _y;
public:
Point(int x, int y);
~Point();
void show();
int x();
int y();
void x(int x);
void y(int y);
};
#endif
#include "polygon.h"
#include "point.h"
#include <iostream>
#include <vector>
Polygon::~Polygon(){}
Polygon::Polygon(){}
std::vector<Point> Polygon::points(){return _points;}
Polygon::Polygon(std::vector<Point> points){_points = points;}
void Polygon::show(){
std::cout << "Points: ";
for(std::vector<Point>::size_type i = 0; i != _points.size(); i++) {
_points[i].show();
}
}
#ifndef POLYGON_GUARD
#define POLYGON_GUARD
#include <vector>
#include "point.h"
class Polygon{
//private:
std::vector<Point> _points;
public:
~Polygon();
Polygon ();
Polygon(std::vector<Point> points);
std::vector<Point> points();
void show();
};
#endif
#include <iostream>
#include <vector>
#include "group.h"
#include "polygon.h"
Group::~Group(){}
Group::Group(std::vector<Polygon> polygons){
_ring = polygons.front();
polygons.erase(polygons.begin());
_holes = polygons;
}
void Group::tryChangingAllX(){
std::vector<Point> points = _ring.points();
for(std::vector<Point>::size_type i = 0; i != points.size(); i++) {
points[i].x(15);
}
}
void Group::show(){
_ring.show();
if(_holes.size()>0){
for(std::vector<Polygon>::size_type i = 0; i != _holes.size(); i++) {
_holes[i].show();
}
}
}
#ifndef GROUP_GUARD
#define GROUP_GUARD
#include <vector>
#include "polygon.h"
class Group{
Polygon _ring;
std::vector<Polygon> _holes;
public:
~Group();
Group(std::vector<Polygon> polygons);
void show();
void tryChangingAllX();
};
#endif
Thanks! 谢谢!
The function 功能
std::vector<Point> points();
returns by value , so when you call it, you get a copy of the member. 按值返回,因此在调用它时,将获得该成员的副本。 You need to change it to 您需要将其更改为
std::vector<Point>& points();
After you've done this 完成此操作后
std::vector<Point> points = _ring.points();
also makes a copy of the returned value. 还会复制返回值。 To refer to the actual member in _ring
, change to: 要引用_ring
的实际成员,请更改为:
std::vector<Point>& points = _ring.points();
That should do it. 那应该做。
Note that you should pass std::vector
by const reference to prevent an un-necessary copy: 请注意,应通过const引用传递std::vector
,以防止不必要的复制:
Polygon(const std::vector<Point>& points);
and consider making methods that don't modify the class const
: 并考虑制作不修改类const
:
int x() const;
This is exactly your problem - you're getting a copy of your points rather than working on a reference to the original points themselves. 这正是您的问题-您得到的是点的副本 ,而不是对原始点本身的引用 。
polygon.cpp: polygon.cpp:
return points by reference not value: 返回点按引用而不是值:
std::vector<Point>& Polygon::points(){return _points;} // note the '&' in the return
group.cpp: group.cpp:
obtain a reference to the points, not a copy 获得要点的参考 ,而不是副本
std::vector<Point>& points = _ring.points(); // note the '&' in what you're getting
Answers posted by Luchian and lori are technically correct. Luchian和lori发布的答案在技术上是正确的。 But there are design considerations that I want to point out. 但是,我要指出一些设计注意事项。
Returning a reference
will allow anyone to modify private
parts of Polygon
object. 返回reference
将允许任何人修改Polygon
对象的private
部分。 By design you only want Group
class to do this. 通过设计,您只希望Group
类这样做。 Consider making Group
a friend
of Polygon
. 考虑让Group
成为Polygon
的friend
。 Group
then will have access to private bits of Polygon
. 然后, Group
将可以访问Polygon
私有位。 This will ensure a tighter encapsulation overall. 这将确保整体封装更紧密。
In polygon.h 在polygon.h中
friend class Group;
In group.cpp 在group.cpp中
void Group::tryChangingAllX()
{
for(std::vector<Point>::size_type i = 0; i != _ring._points.size(); i++)
{
_ring._points[i].x(15);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.