简体   繁体   中英

Operator overload = and const reference

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...

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 Color s. 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;
}

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);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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