运算符重载 = 和 const 引用

发布于 2024-12-11 01:50:30 字数 1251 浏览 0 评论 0原文

我正在尝试用 C++ 构建一个颜色类,
这不是作业,只是我仍在与引用和 const 作斗争。

--Color.h

class Color{
private:
    double r;
    double g;
    double b;
    double a;
public:
    //constructor, getters and setters...
    Color& operator =(Color& other_color); //(1)
}

--Color.cpp

Color& operator=(Color& other_color){
     this->r = other_color.get_r(); //line 41
     this->b = other_color.get_b();
     //and so on...
     return *this;
}

像这样工作正常,但我听说必须放置一个 const 以避免由于错误而导致对象被赋值操作修改,因此必须将另一个对象声明为 const。像这样:

Color& operator =(Color const& other_color); //(2)

但它给了我这个错误:

/Users/../color.cpp:41: error: passing 'const Color' as 'this' argument of 'float Color::get_r()' discards qualifiers

所以这是我的问题......

这里发生了什么?其次,如果我不将 other_color 声明为 const 会发生什么?可能的错误是什么?

PS.:小奖励问题:
我想将变量传递给 opengl glColor4v(colorx.return_rgba()) 返回 Color 类的数组 [r,g,b,a] 。这:

float* Color::return_rgba(){
    float  rgba[4] = {this->r, this->g, this->b, this->a};
    return rgba;
}

不起作用,因为返回后 rgba 将不再处于范围内,因此它将被删除,我的指针将指向未初始化的地址,该死的......

I'm trying to construct a class for colors in C++,
this is not an homework is just that I'm still struggling with references and const.

--Color.h

class Color{
private:
    double r;
    double g;
    double b;
    double a;
public:
    //constructor, getters and setters...
    Color& operator =(Color& other_color); //(1)
}

--Color.cpp

Color& operator=(Color& other_color){
     this->r = other_color.get_r(); //line 41
     this->b = other_color.get_b();
     //and so on...
     return *this;
}

like this it works fine but I heard one has to put a const to avoid that by fault the object will be modified by the assignement operation, so one has to declare the other object as const. Like this:

Color& operator =(Color const& other_color); //(2)

but it gives me this errors:

/Users/../color.cpp:41: error: passing 'const Color' as 'this' argument of 'float Color::get_r()' discards qualifiers

so here is my question...

what is happening here? second what would happen if I don't declare other_color as const? what are the possible errors?

PS.: little bonus question:
I want to pass my variable to the opengl glColor4v(colorx.return_rgba()) returning the array [r,g,b,a] of the class Color. This:

float* Color::return_rgba(){
    float  rgba[4] = {this->r, this->g, this->b, this->a};
    return rgba;
}

won't work because rgba won't be in scope anymore after the return so it will be deleted and my pointer will point to not initialized adresses, damn...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

童话里做英雄 2024-12-18 01:50:30

将“const Color”作为“float Color::get_r()”的“this”参数传递会丢弃限定符

这意味着您必须更进一步。 get_r 可能被声明为

float get_r()

并使其工作(const-正确),你应该使它

float get_r() const

第二,如果我不将 other_color 声明为 const 会发生什么?

您将无法从 const 限定的 Color 进行分配。您通常希望能够使用 const 对象等作为赋值源。此外,它使代码读者清楚不修改源代码的意图。

我想将我的变量传递给 opengl glColor4v(colorx.return_rgba()) 返回 Color 类的数组 [r,g,b,a]。

返回一个特殊的“车辆”,其中包含该数组并自动转换为 float*。一些东西沿着

struct ColorQuadruplet
{
  float data_[4];
  // add initialization and such here
  operator float*() { return data_; }
};

ColorQuadruplet Color::get_rgba() const
{
  ColorQuadruplet ret;
  // fill ret
  return ret;
}

passing 'const Color' as 'this' argument of 'float Color::get_r()' discards qualifiers

This means you have to take it further. get_r is probably declared as

float get_r()

and to make it work (const-correctly), you should make it

float get_r() const

second what would happen if I don't declare other_color as const?

You would be unable to assign from const-qualified Colors. You usually want to be able to use const objects, among other as source of assignment. Moreover, it makes the intent not to modify the source clear to the reader of the code.

I want to pass my variable to the opengl glColor4v(colorx.return_rgba()) returning the array [r,g,b,a] of the class Color.

Return a special "vehicle" that would contain the array and convert automatically to float*. Something along

struct ColorQuadruplet
{
  float data_[4];
  // add initialization and such here
  operator float*() { return data_; }
};

ColorQuadruplet Color::get_rgba() const
{
  ColorQuadruplet ret;
  // fill ret
  return ret;
}
无尽的现实 2024-12-18 01:50:30

这里你有两个选择。一种是让您的 operator= 直接访问源对象的成员:

Color &operator=(Color const &other) { 
    r = other.r;
    g = other.g;
    b = other.b;
    a = other.a;
}

另一种(如果您坚持拥有颜色组件的访问器,您可能在任何情况下都想要这样做)是对您编写的访问器进行常量限定:

double get_r() const { return r; }
               ^^^^^

这里的 const 是我添加的部分,但您显然没有。

编辑:就将值传递给 glColor 而言,我会考虑一个像这样的小型前端:

gl_color(Color const &c) { 
    glColor4d(c.r, c.g, c.b, c.a);
}

You have two choices here. One is for your operator= to directly access to the members of the source object:

Color &operator=(Color const &other) { 
    r = other.r;
    g = other.g;
    b = other.b;
    a = other.a;
}

The other (which you probably want to do in any case, if you insist on having accessors for the color components at all) is to const-qualify the accessors you've written:

double get_r() const { return r; }
               ^^^^^

The const here is the part I've added that you apparently don't have.

Edit: as far as passing the values to glColor goes, I'd consider a small front-end something like this:

gl_color(Color const &c) { 
    glColor4d(c.r, c.g, c.b, c.a);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文