运算符重载 = 和 const 引用
我正在尝试用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这意味着您必须更进一步。
get_r
可能被声明为并使其工作(const-正确),你应该使它
您将无法从
const
限定的Color
进行分配。您通常希望能够使用 const 对象等作为赋值源。此外,它使代码读者清楚不修改源代码的意图。返回一个特殊的“车辆”,其中包含该数组并自动转换为
float*
。一些东西沿着This means you have to take it further.
get_r
is probably declared asand to make it work (const-correctly), you should make it
You would be unable to assign from
const
-qualifiedColor
s. You usually want to be able to useconst
objects, among other as source of assignment. Moreover, it makes the intent not to modify the source clear to the reader of the code.Return a special "vehicle" that would contain the array and convert automatically to
float*
. Something along这里你有两个选择。一种是让您的
operator=
直接访问源对象的成员:另一种(如果您坚持拥有颜色组件的访问器,您可能在任何情况下都想要这样做)是对您编写的访问器进行常量限定:
这里的
const
是我添加的部分,但您显然没有。编辑:就将值传递给 glColor 而言,我会考虑一个像这样的小型前端:
You have two choices here. One is for your
operator=
to directly access to the members of the source object: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:
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: