从另一个类中修改对象的std :: vector

时间:2012-11-26 04:17:49

标签: c++ pass-by-reference

说明:

在我的程序中,group对象有std::vector个多边形(一个圆环和一个孔),一个polygon个对象有std::vector个点。实际上,课程更复杂,我把它们剥离在这里以说明我的问题。

问题:

我希望能够从其对应的组对象中修改point的x和y坐标。下面,在group.cpp中,我有一个名为void Group::tryChangingAllX()的虚函数,试图完成此任务。但是,之后在组上调用show()表示它的多边形点的坐标没有变化。

我想我需要使用引用/指针,但我需要在正确的方向上轻推。

point.cpp:

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

point.h:

#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

polygon.cpp:

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

polygon.h:

#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

group.cpp:

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

group.h:

#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

谢谢!

3 个答案:

答案 0 :(得分:5)

功能

std::vector<Point> points();

按值返回 ,因此当您调用它时,您会获得该成员的副本。您需要将其更改为

std::vector<Point>& points();

完成此操作后

std::vector<Point> points = _ring.points();

还会复制返回的值。要引用_ring中的实际成员,请更改为:

std::vector<Point>& points = _ring.points();

应该这样做。

请注意,您应该通过const引用传递std::vector以防止不必要的副本:

Polygon(const std::vector<Point>& points); 

并考虑制作不修改类const的方法:

int x() const;  

答案 1 :(得分:1)

这正是你的问题 - 你得到了你的积分的副本,而不是自己的引用

<强> polygon.cpp:

通过参考返回积分而不是值:

std::vector<Point>& Polygon::points(){return _points;} // note the '&' in the return

<强> group.cpp:

获得点的引用,而不是副本

std::vector<Point>& points = _ring.points(); // note the '&' in what you're getting

答案 2 :(得分:0)

Luchian和Lori发布的答案在技术上是正确的。但是我想指出一些设计考虑因素。

返回reference将允许任何人修改privatePolygon部分对象。按照设计,您只需要Group类来执行此操作。考虑将Group friend作为PolygonGroup然后可以访问Polygon的私有位。这将确保整体封装更紧密。

polygon.h

friend class Group;

group.cpp

void Group::tryChangingAllX()
{
    for(std::vector<Point>::size_type i = 0; i != _ring._points.size(); i++)
    {
        _ring._points[i].x(15);
    }
}