【问题标题】:How can I create the getter and setter for the vector?如何为向量创建 getter 和 setter?
【发布时间】:2020-05-02 22:39:45
【问题描述】:

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 文件

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

我正在尝试创建一个由 2 个点组成的向量。我怎样才能创建他们的吸气剂和定居者?我想我创建了 setter 函数,但我不确定。 注意:GeoBox 我的文件名

【问题讨论】:

  • 是的,这是一个 setter 函数。如果将点放入结构或数组中,则可以简单地在 getter 函数中返回该数组
  • 请问您可以输入一下吗?

标签: c++ getter-setter


【解决方案1】:

要创建向量,您应该将其初始化为模板类型。

对于 setter 函数,您必须实现可以接收两个迭代器的 erase() 函数,一个指向要删除的值,如果要删除指向初始值和最后一个值的元素范围,则需要两个迭代器。 erase()函数已经是c++11中实现的函数,但是可以开发。

【讨论】:

    【解决方案2】:

    这似乎可行。为了提高效率,我将您的类更改为存储指针而不是对象。当然,我无法对此进行测试,因为我没有您的其余代码,也无法保证您的设置。

    #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
    

    要使用结构(在这种情况下不使用指针)执行此操作,您可以这样设置它:

    #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
    

    在这种情况下仍然可以(并且应该)使用指针,但是因为它们可以提高程序的整体速度。

    【讨论】:

    • 我们可以在不更改指针的情况下获得 getter 吗?因为point2d上的函数已经根据对象设置了。
    猜你喜欢
    • 2014-08-05
    • 1970-01-01
    • 2019-04-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多