繁体   English   中英

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

[英]modifying an object's std::vector from within another class

介绍:

在我的程序中, group对象的std::vector充满了多边形(一个圆环和孔),而polygon对象的点则是std::vector 实际上,这些类更为复杂,我在这里将它们简化了下来以说明我的问题。

问题:

我希望能够从其相应的组对象中修改point 'sx和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

谢谢!

功能

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;  

这正是您的问题-您得到的是点的副本 ,而不是对原始点本身的引用

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

Luchian和lori发布的答案在技术上是正确的。 但是,我要指出一些设计注意事项。

返回reference将允许任何人修改Polygon对象的private部分。 通过设计,您只希望Group类这样做。 考虑让Group成为Polygonfriend 然后, Group将可以访问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);
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM