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

标签 c++ pass-by-reference

介绍:

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

问题:

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

我认为我需要使用引用/指针,但我需要朝着正确的方向插入。

点.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;}

点.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

多边形.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();
    }
}

多边形.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

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

组.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;  

关于c++ - 从另一个类中修改对象的 std::vector,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13558578/

相关文章:

Java数组:Java pass by reference and pass by value?

java - 重访按值调用与按引用调用

C++ 64 位整数 : pass by reference or pass by value

C++:声明一个具有处理字符串的函数的类

C++:如何分配和填充通过引用传递的结构的动态数组?

C++通过引用传递然后设置指向对象的指针

c++ - 析构函数导致程序崩溃

c++ - modf() 函数的定义?

c++ - QT 使用接受的信号连接崩溃

c++ - 宏定义如何用于内存泄漏检测?