【问题标题】:Updating a variable in a struct更新结构中的变量
【发布时间】:2022-01-01 04:54:36
【问题描述】:

所以我刚刚创建了一个构成矩形的结构。结构本身看起来像这样

    struct _rect
{
    //bottom left vertex
    int x = 0;
    int y = 0;

    // width and height 
    int width = 0;
    int height = 0;

    //top right vertex
    int y2 = y + height;
    int x2 = x + width; 
};

//init rect
_rect rect01;
rect01.x = rect01.y = 50;
rect01.width = rect01.height = 200;

在主cpp中,当我想创建它的一个实例时,我只想输入左下角x和y,加上宽度和高度,我希望它自己计算右上角的顶点,有没有办法分配x2和 y2 他们的价值观没有手动这样做?

【问题讨论】:

  • 你从你的结构名称开始未定义的行为:以下划线开头的名称被保留在全局命名空间中......
  • 你能解释一下 x2,y2 的需要吗?矩形需要一个点和高度和宽度来绘制
  • Constructor 是这里的关键字:struct Rect { Rect(int x, int y, int w, int h) : x(x), y(y), width(w), height(h), y2(y+h), x2(x + w) { } }; - 但请注意,如果您更改其中一个成员,成员不会自动更新 - 我会这样做仅当您将所有这六个值设为const 或将它们设为private 并提供适当的 getter 和 setter 为您进行更新时,才可以这样做。否则,我宁愿提供x2 和y2 作为按需进行必要计算的函数。选择哪一个?取决于用例,但在大多数情况下取决于功能。
  • @463035818_is_not_a_number 你是对的,删除了这个建议
  • @Mo_AminKi​​aee,问题是,为什么需要存储 x2 和y2?为什么不即时计算它们呢?一个矩形完全由四个数字指定(x 和 y 以及宽度和高度,或者 x1,y1 和 x2,y2。)存储六个数字是多余的,如果有人在没有修改代码的情况下修改代码,则会增加程序中出现错误的风险了解代码的哪一部分负责确保这六个数字彼此一致。

标签: c++ struct


【解决方案1】:

你应该创建一个特定的类:

class Rect
{
public:
  Rect(int x, int y, unsigned int width, unsigned int height)
  : m_x(x), m_y(y), m_width(width), m_height(height)
  {}

  int x() { return m_x; }
  int y() { return m_y; }
  int top() { return m_y + m_height; }
  int right() { return m_x + m_width; }

private:
  int m_x;
  int m_y;
  unsigned int m_width;
  unsigned int m_height;
};

这使您可以在类方法中进行所需的计算。 如果需要,您还可以创建 setter 和更多 getter。

【讨论】:

  • 在构造函数体中初始化优于赋值 (stackoverflow.com/questions/1711990/…)
  • 正确的想法,但在构造函数中使用初始化列表。 Rect(int x, int y, unsigned width, unsigned height) : x(x), y(y), width(width), height(height) { ... }。但是,当然,实际代码不会为参数和数据成员使用相同的名称。 +1。
  • 变量和getter需要不同的标识符。
【解决方案2】:

以下是完整的工作示例:

#include <iostream>
class Rect
{
public:
  //parameterized constructor 
  Rect(int px, int py, unsigned int pwidth, unsigned int pheight): x(px), y(py), width(pwidth), height(pheight), x2(x + width), y2(y + height)
  {
    
    
   
  };
  
  //getter so that we can get the value of x2 
  int getX2()
  {
      return x2;
  }
  //getter so that we can get the value of y2 
  int getY2()
  {
      return y2;
  }

private:
  int x = 0;
  int y = 0;
  unsigned int width = 0;
  unsigned int height = 0;
  
  int x2 = 0, y2 = 0;
};

int main()
{
    //create Rect instance
    Rect r(50, 50, 200, 200);
    //lets check if x2 and y2 were calculate correctly
    std::cout<<"x2 is: "<< r.getX2()<<std::endl;
    std::cout<<"y2 is: "<< r.getY2()<<std::endl;
}

以上程序的输出可见here。

【讨论】:

  • 我怀疑问题不在于创建只读类。另外,x2 和 y2 应该在初始化列表中初始化。
  • @PeteBecker NO。阅读 OP 问题的最后一句话。他/她写道:“我想创建它的一个实例,我只想输入左下角 x 和 y,加上宽度和高度,我希望它自己计算右上角的顶点,有没有办法分配 x2 和 y2 他们的值没有手动这样做?”所以OP想要创建实例并且只提供变量x,y, height, width。接下来他/她要自动计算变量x2, y2。
  • 这是一个非常狭隘的问题解读。
  • @PeteBecker 根据引用的句子,这正是 OP 想要的。
  • @Aconcagua 是的 x2 和 y2 应该使用构造函数初始化列表进行初始化,我同意这一点。但请注意,根据您的解释,接受的答案也有一个read only。
猜你喜欢
  • 2014-03-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-06-14
  • 1970-01-01
  • 2022-11-05
  • 2021-12-17
  • 1970-01-01
相关资源
最近更新 更多