简体   繁体   English

我可以将类成员变量设置为类函数的结果吗?

[英]Can I set a class member variable to the result of a class function?

I have two functions in a Vector class, x() and y() that return the corresponding component from the vector. 我在Vector类中有两个函数, x()y()从向量返回相应的分量。 I want to be able to access the component using a member variable instead (or as well). 我希望能够使用成员变量代替(或同样)访问组件。

Previously I made x and y pointers that point directly into the array of components, data - but this isn't a good solution because someone could change the pointer, right? 以前,我制作了xy指针,它们直接指向组件数组, data -但这不是一个好的解决方案,因为有人可以更改指针,对吗? And it would need to be dereferenced every time it is used. 并且每次使用时都需要取消引用。

I cannot use a union due to how my classes are structured. 由于类的结构,我无法使用联合。

Currently my idea is to have the member variables x and y return the result of functions x() and y() . 目前,我的想法是让成员变量xy返回函数x()y() Is this a feasible solution? 这是可行的解决方案吗?

template <typename T>
class Vec2 : public VecBase<T, 2>
{
public:
    T& x = x();     //Doesn't work but is what I'm trying to achieve
    T& y = y();

    T& x() const { return data[0]; }
    T& y() const { return data[1]; }
}

EDIT: Should have made it more obvious - VecBase is a variable sized vector (vector2, vector3, etc.). 编辑:应该使它更加明显VecBase是一个可变大小的向量(vector2,vector3等)。 Vec2 adds x and y when the size is 2 to make accessing the components easier. 当大小为2时, Vec2xy相加,以使访问组件更加容易。 It's hard to show without pasting all the code. 如果不粘贴所有代码,很难显示出来。 Ha. 哈。

The outcome I want is to basically be able to access the components of the vector easily like so: 我想要的结果是基本上能够像这样简单地访问向量的组成部分:

Vec2<int> vector;
vector.x = 20;
vector.y = 40;

The functions x() and y() aren't necessary in the solution. 函数x()y()在解决方案中不是必需的。

Thanks for your help! 谢谢你的帮助!

Your reference member approach can work: 您的参考成员方法可以起作用:

template <typename T>
class Vec2 : public VecBase<T, 2>
{
public:
    Vec2() : x(this->data[0]), y(this->data[1]) {}

    T& x;
    T& y;
};

However, now you have a user-defined constructor. 但是,现在您有了用户定义的构造函数。 Plus there's still going to be a dereference under the bonnet. 此外,在引擎盖下仍将存在取消引用的情况。 Personally I'd avoid this over-complication entirely, if possible. 如果可能的话,我个人将完全避免这种过度复杂化。

Although I understand that you want to implement the guts of it all in a generic way, and that your base can't "automatically" name the members in a nice mathsy way, consider whether you really need this. 尽管我知道您想以一种通用的方式来实现这一切,并且您的基地无法以一种很好的数学方式来“自动”命名成员,但是请考虑您是否真的需要这样做。 Why not just a couple of Vec2 , Vec3 types that literally just have direct members in them? 为什么不只是几个Vec2Vec3类型,它们实际上只是直接成员呢? Do you really need anything more? 您真的还需要更多吗?

Having T as a template parameter is a good idea though. 不过,以T作为模板参数是一个好主意。

I want to be able to access the component using a member variable 我希望能够使用成员变量访问组件

Then simply use a struct (or class with public members): 然后只需使用一个struct (或具有public成员的class ):

template <typename T>
struct Vector
{ 
    T x;
    T y;
};

Vector<int> v;
v.x = 42;
v.y = data[2];

You can achieve almost what you want without the reference members. 没有参考成员,您几乎可以实现所需的目标。 You can assign to the result of x() . 您可以分配给x()的结果。

Vec2<int> vector;
vector.x() = 20;
vector.y() = 40;

Note that you shouldn't allow modifying through a const Vec2 , so overload x and y based on the const qualification. 请注意,您不应允许通过const Vec2进行修改,因此应根据const限定值重载xy

template <typename T>
class Vec2 : public VecBase<T, 2>
{
public:
    const T& x() const { return data[0]; }
    const T& y() const { return data[1]; }

    T& x() { return data[0]; }
    T& y() { return data[1]; }
};

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

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