简体   繁体   English

运算符重载=和const参考

[英]Operator overload = and const reference

I'm trying to construct a class for colors in C++, 我正在尝试为C ++中的颜色构造一个类,
this is not an homework is just that I'm still struggling with references and const. 这不是一个作业,只是我仍在努力使用引用和const。

--Color.h --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.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. 像这样,它可以正常工作,但是我听说一个人必须放置一个const以避免由于错误而该对象将被赋值操作修改,因此一个人必须将另一个对象声明为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? 第二,如果我不将other_color声明为const会发生什么? what are the possible errors? 有哪些可能的错误?

PS.: little bonus question: PS .:小奖金问题:
I want to pass my variable to the opengl glColor4v(colorx.return_rgba()) returning the array [r,g,b,a] of the class Color. 我想将变量传递给opengl glColor4v(colorx.return_rgba()),以返回Color类的数组[r,g,b,a]。 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... 将不起作用,因为rgba在返回后将不在范围内,因此它将被删除,并且我的指针将指向未初始化的地址,该死的...

passing 'const Color' as 'this' argument of 'float Color::get_r()' discards qualifiers 将'const Color'传递为'float Color :: get_r()'的'this'参数时,将丢弃限定符

This means you have to take it further. 这意味着您必须更进一步。 get_r is probably declared as get_r可能声明为

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? 第二,如果我不将other_color声明为const会发生什么?

You would be unable to assign from const -qualified Color s. 您将无法从const限定的Color进行分配。 You usually want to be able to use const objects, among other as source of assignment. 通常,您希望能够使用const对象以及其他作为分配源的对象。 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. 我想将变量传递给opengl glColor4v(colorx.return_rgba()),以返回Color类的数组[r,g,b,a]。

Return a special "vehicle" that would contain the array and convert automatically to float* . 返回一个特殊的“车辆”,该车辆将包含数组并自动转换为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;
}

You have two choices here. 您在这里有两个选择。 One is for your operator= to directly access to the members of the source object: 一种是让您的operator=直接访问源对象的成员:

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: 另一种方法(如果您坚持要完全使用颜色组件的访问器,无论如何都可能要这样做)是对您编写的访问器进行const限定:

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

The const here is the part I've added that you apparently don't have. 这里的const是我添加的您显然没有的部分。

Edit: as far as passing the values to glColor goes, I'd consider a small front-end something like this: 编辑:就将值传递给glColor而言,我会考虑一个小的前端,如下所示:

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

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

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