簡體   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