简体   繁体   English

如何为向量创建 getter 和 setter?

[英]How can I create the getter and setter for the vector?

Vector2D hpp files Vector2D hpp 文件

#ifndef Vector2D_hpp
#define Vector2D_hpp

#include <iostream>
#include "Point2D.hpp"

namespace GeoBox{

class Vector2D{

      Point2D m_point1{};
      Point2D m_point2{};

public:
      Vector2D() = default;
      Vector2D(Point2D &point1, Point2D &point2)
{
    m_point1 = point1;
    m_point2 = point2;
}
void setVector(Point2D &point1, Point2D &point2);

};

Vector2D.cpp files Vector2D.cpp 文件

#include "Vector2D.hpp"
#include "Point2D.hpp"


void GeoBox::Vector2D::setVector(GeoBox::Point2D &point1, GeoBox::Point2D &point2) {
    Vector2D(point1, point2);

}

main.cpp主文件

int main(int argc, const char * argv[]) {

GeoBox::Point2D point1{3.0, 1.0};
GeoBox::Point2D point2{2.0, 3.0};
GeoBox::Vector2D vect1{point1, point2};
}

I am trying to create a vector consisting of 2 points.我正在尝试创建一个由 2 个点组成的向量。 how can i create their getters and settlers?我怎样才能创建他们的吸气剂和定居者? I think I created the setter function, but I'm not sure.我想我创建了 setter function,但我不确定。 note:GeoBox my file name注意:GeoBox 我的文件名

To create a vector, you should initialize it as a template type.要创建向量,您应该将其初始化为模板类型。

For the setter function you must implement the erase () function that can receive two iterators, one that points to the value you want to delete or two iterators if you want to delete an element range pointing to the initial and last value.对于设置器 function,您必须实现可以接收两个迭代器的擦除 () function,一个指向要删除的值,如果要删除指向初始值和最后一个值的元素范围,则使用两个迭代器。 The erase () function is already a function implemented in c ++ 11 but it can be developed.擦除()function已经是c++ 11中实现的function,但可以开发。

Its seems like this might work.看起来这可能会奏效。 I changed your class to store pointers and not objects for efficiency.我将您的 class 更改为存储指针而不是对象以提高效率。 Granted, I can't test that this because I don't have the rest of your code and can't guarantee your setup.当然,我无法对此进行测试,因为我没有您的代码的 rest 并且不能保证您的设置。

#ifndef Vector2D_hpp
#define Vector2D_hpp

#include <iostream>
#include "Point2D.hpp"

namespace GeoBox {

    class Vector2D {
        Point2D* points[2];

    public:
        Vector2D() = default;
        Vector2D(Point2D* point1, Point2D* point2)
        {
            setVector(point1, point2);
        }
        void setVector(Point2D* point1, Point2D* point2) {
            points[0] = point1;
            points[1] = point2;
        }

        Point2D* getVector() {
            return points;
        }
    };
}
#endif

To do this with a struct (and without pointers in this case) you would set it up like this:要使用结构(在这种情况下不使用指针)执行此操作,您可以像这样设置它:

#ifndef Vector2D_hpp
#define Vector2D_hpp

#include <iostream>
#include "Point2D.hpp"

namespace GeoBox {

    class Vector2D {
        struct Vector { //Define the struct
            Point2D point1;
            Point2D point2;
        } points;   //Create instance of struct

    public:
        Vector2D() = default;
        Vector2D(Point2D point1, Point2D point2)
        {
            setVector(point1, point2);
        }
        void setVector(Point2D i_point1, Point2D i_point2) {
            points.point1 = i_point1; //Access struct components
            points.point2 = i_point2;
        }

        Vector getVector() {
            return points;  //return struct
        }
    };
}
#endif

Pointers can (and should) still be used in this case however as they allow for increased speed in your program overall.在这种情况下仍然可以(并且应该)使用指针,但是因为它们可以提高程序的整体速度。

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

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