简体   繁体   English

在 SAME 结构中使用结构作为 function 的返回类型是常见的做法吗

[英]Is it common practice to Use a struct as the return type for a function inside the SAME struct

Its kind hard for me to describe it so here's the code:我很难描述它,所以这里是代码:

#if !defined(OLC_IGNORE_VEC2D)
template <class T>
struct v2d_generic
{
    T x = 0;
    T y = 0;
    inline v2d_generic() : x(0), y(0)                        {                                                            }
    inline v2d_generic(T _x, T _y) : x(_x), y(_y)            {                                                            }
    inline v2d_generic(const v2d_generic& v) : x(v.x), y(v.y){                                                            }
    inline T mag()                                           { return std::sqrt(x * x + y * y);                           }
    inline T mag2()                                          { return x * x + y * y;                                      }
    **inline v2d_generic  norm()                               { T r = 1 / mag(); return v2d_generic(x*r, y*r);             }
    inline v2d_generic  perp()                               { return v2d_generic(-y, x);                                 }
    inline T dot(const v2d_generic& rhs)                     { return this->x * rhs.x + this->y * rhs.y;                  }
    inline T cross(const v2d_generic& rhs)                   { return this->x * rhs.y - this->y * rhs.x;                  }**
    inline v2d_generic  operator +  (const v2d_generic& rhs) const { return v2d_generic(this->x + rhs.x, this->y + rhs.y);}
    inline v2d_generic  operator -  (const v2d_generic& rhs) const { return v2d_generic(this->x - rhs.x, this->y - rhs.y);}
    inline v2d_generic  operator *  (const T& rhs)           const { return v2d_generic(this->x * rhs, this->y * rhs);    }
    inline v2d_generic  operator *  (const v2d_generic& rhs) const { return v2d_generic(this->x * rhs.x, this->y * rhs.y);}
    inline v2d_generic  operator /  (const T& rhs)           const { return v2d_generic(this->x / rhs, this->y / rhs);    }
    inline v2d_generic  operator /  (const v2d_generic& rhs) const { return v2d_generic(this->x / rhs.x, this->y / rhs.y);}
    inline v2d_generic& operator += (const v2d_generic& rhs) { this->x += rhs.x; this->y += rhs.y; return *this;          }
    inline v2d_generic& operator -= (const v2d_generic& rhs) { this->x -= rhs.x; this->y -= rhs.y; return *this;          }
    inline v2d_generic& operator *= (const T& rhs)           { this->x *= rhs; this->y *= rhs; return *this;              }
    inline v2d_generic& operator /= (const T& rhs)           { this->x /= rhs; this->y /= rhs; return *this;              }
    inline operator v2d_generic<int32_t>() const { return { static_cast<int32_t>(this->x), static_cast<int32_t>(this->y) }; }
    inline operator v2d_generic<float>() const { return { static_cast<float>(this->x), static_cast<float>(this->y) };     }
    inline operator v2d_generic<double>() const { return { static_cast<double>(this->x), static_cast<double>(this->y) };  }
};

Is it normal for a function inside a struct to have the return type as the struct itself?:结构内的 function 将返回类型作为结构本身是否正常?

inline v2d_generic  norm()                               { T r = 1 / mag(); return v2d_generic(x*r, y*r);             }
    inline v2d_generic  perp()                               { return v2d_generic(-y, x);
      

Also I'm kinda confused as to what exactly these lines are doing:另外,我对这些行到底在做什么感到有些困惑:

inline v2d_generic  operator +  (const v2d_generic& rhs) const { return v2d_generic(this->x + rhs.x, this->y + rhs.y);}
inline v2d_generic  operator -  (const v2d_generic& rhs) const { return v2d_generic(this->x - rhs.x, this->y - rhs.y);}
inline v2d_generic  operator *  (const T& rhs)           const { return v2d_generic(this->x * rhs, this->y * rhs);    }
inline v2d_generic  operator *  (const v2d_generic& rhs) const { return v2d_generic(this->x * rhs.x, this->y * rhs.y);}
inline v2d_generic  operator /  (const T& rhs)           const { return v2d_generic(this->x / rhs, this->y / rhs);    }
inline v2d_generic  operator /  (const v2d_generic& rhs) const { return v2d_generic(this->x / rhs.x, this->y / rhs.y);}
inline v2d_generic& operator += (const v2d_generic& rhs) { this->x += rhs.x; this->y += rhs.y; return *this;          }
inline v2d_generic& operator -= (const v2d_generic& rhs) { this->x -= rhs.x; this->y -= rhs.y; return *this;          }
inline v2d_generic& operator *= (const T& rhs)           { this->x *= rhs; this->y *= rhs; return *this;              }
inline v2d_generic& operator /= (const T& rhs)           { this->x /= rhs; this->y /= rhs; return *this; 

:) :)

The functions in question are all overridden operators.有问题的函数都是被覆盖的运算符。 These allow c++ to know what happens when you add v2d_generic_1 + v2d_generic_2 for example.例如,这些允许 c++ 知道添加v2d_generic_1 + v2d_generic_2时会发生什么。

The reason they return a value of type v2d_generic is because that's how the + , - ,etc operators need to work if you want types that behave in a number like way.它们返回v2d_generic类型值的原因是,如果您希望类型以类似数字的方式运行,这就是+-等运算符的工作方式。 A + B must return the result of that operator for example so that this result could be then used in the calculation (A + B) * C .例如, A + B必须返回该运算符的结果,以便该结果可以用于计算(A + B) * C

The result of this calculation would also be a v2d_generic and so on..此计算的结果也将是v2d_generic等等。

Is it normal for a function inside a struct to have the return type as the struct itself?:结构内的 function 将返回类型作为结构本身是否正常?

Yes, it is.是的。 In most cases used for operator overloading and generating new instances of struct or class based on current instance of struct or class.在大多数情况下,用于运算符重载和基于结构或 class 的当前实例生成新的结构或 class 实例。

Also I'm kinda confused as to what exactly these lines are doing:另外,我对这些行到底在做什么感到有些困惑:

search about operator overloading in c++.在 c++ 中搜索有关运算符重载的信息。 cppreference.com operator overloading cppreference.com 运算符重载

The answer is yes, just as it is common to return class instances from class member functions.答案是肯定的,就像通常从 class 成员函数返回 class 实例一样。

The operator lines just simply implement the new operators on the defined type v2d_generic.运算符行只是简单地在定义的类型 v2d_generic 上实现新的运算符。 The same way you can create two strings - stringA and stringB - and concatenate them by using stringA + stringB because the std::string class implements the operator+.同样的方式,您可以创建两个字符串 - stringA 和 stringB - 并使用 stringA + stringB 连接它们,因为 std::string class 实现了 operator+。

std::string stringA("Hello"), stringB(" StackOverflow"), result;
result = stringA + stringB;

This is possible because std::string implements the operator+.这是可能的,因为 std::string 实现了 operator+。

See here: std::string operator+见这里: std::string operator+

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

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