如何修改给定类以使用 const 运算符

发布于 2024-12-18 03:01:52 字数 1476 浏览 1 评论 0原文

我正在尝试解决我的问题关于在多个级别使用push_back。从评论/答案中可以清楚地看出,我必须:

  1. 创建一个采用 const 参数的复制运算符
  2. 将所有运算符修改为 const

但因为这个头文件是给我的,所以有一个运算符我无法将其变为 const。这很简单:

float & operator [] (int i) {
    return _item[i];
}

在给定的程序中,该运算符用于获取和设置数据。

我的问题是,因为我需要在头文件中包含此运算符,所以我无法将所有其他运算符转换为 const,这意味着我无法插入复制运算符。

如何将所有运算符都变为 const,同时保留已编写程序的功能?

以下是该类的完整声明:

class Vector3f {

    float _item[3];

    public:

    float & operator [] (int i) {
        return _item[i];
        }

    Vector3f(float x, float y, float z) 
    {  _item[0] = x ; _item[1] = y ; _item[2] = z; };

    Vector3f() {};

    Vector3f & operator = ( const Vector3f& obj) 
    {
        _item[0] = obj[0];
        _item[1] = obj[1];
        _item[2] = obj[2];

        return *this;
    };

    Vector3f & operator += ( const Vector3f & obj) 
    {
        _item[0] += obj[0];
        _item[1] += obj[1];
        _item[2] += obj[2];

        return *this;
    };

    bool operator ==( const Vector3f & obj) {
        bool x = (_item[0] == obj[0]) && (_item[1] == obj[1]) && (_item[2] == obj[2]);
        return x;
    }


    // my copy operator
    Vector3f(const Vector3f& obj) {
        _item[0] += obj[0];
        _item[1] += obj[1];
        _item[2] += obj[2];

        return this;
    }

};

I am trying to solve my question regarding using push_back in more than one level. From the comments/answers it is clear that I have to:

  1. Create a copy operator which takes a const argument
  2. Modify all my operators to const

But because this header file is given to me there is an operator what I cannot make into const. It is a simple:

float & operator [] (int i) {
    return _item[i];
}

In the given program, this operator is used to get and set data.

My problem is that because I need to have this operator in the header file, I cannot turn all the other operators to const, what means I cannot insert a copy operator.

How can I make all my operators into const, while preserving the functionality of the already written program?

Here is the full declaration of the class:

class Vector3f {

    float _item[3];

    public:

    float & operator [] (int i) {
        return _item[i];
        }

    Vector3f(float x, float y, float z) 
    {  _item[0] = x ; _item[1] = y ; _item[2] = z; };

    Vector3f() {};

    Vector3f & operator = ( const Vector3f& obj) 
    {
        _item[0] = obj[0];
        _item[1] = obj[1];
        _item[2] = obj[2];

        return *this;
    };

    Vector3f & operator += ( const Vector3f & obj) 
    {
        _item[0] += obj[0];
        _item[1] += obj[1];
        _item[2] += obj[2];

        return *this;
    };

    bool operator ==( const Vector3f & obj) {
        bool x = (_item[0] == obj[0]) && (_item[1] == obj[1]) && (_item[2] == obj[2]);
        return x;
    }


    // my copy operator
    Vector3f(const Vector3f& obj) {
        _item[0] += obj[0];
        _item[1] += obj[1];
        _item[2] += obj[2];

        return this;
    }

};

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

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

发布评论

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

评论(2

留一抹残留的笑 2024-12-25 03:01:52

我不太明白你想要做什么,但我注意到这段代码不可能编译。原因是复制是由复制构造函数处理的,而不是操作符。这意味着,像任何构造函数一样,它不会返回任何内容。从构造函数中删除 return 语句,如下所示:

Vector3f(const Vector3f& obj) {
    _item[0] += obj[0];
    _item[1] += obj[1];
    _item[2] += obj[2];
}

至于使运算符 const,您可以简单地重载它并提供同一方法的两个版本。第一个将是非 const 并将返回一个引用(允许修改),而第二个将是 const 并返回一个副本(理想情况下您应该返回一个>const 引用,但由于 float 是原始类型,因此只需按值返回)。

float & operator [] (int i) 
{
    return _item[i];
}
float operator [] (int i) const 
{
    return _item[i];
}

I did not really understand what you're trying to do, but I noticed that this code can't possibly compile. The reason is that copy is handled by a copy constructor, not operator. Which means that, like any constructor, it doesn't return anything. Remove the return statement from your constructor, like so:

Vector3f(const Vector3f& obj) {
    _item[0] += obj[0];
    _item[1] += obj[1];
    _item[2] += obj[2];
}

As for making your operator const, you can simply overload it and offer two versions of the same method. The first one will be non-const and will return a reference (allowing modifications), while the second will be const and return a copy (ideally you should return a const reference, but since floats are primitive types, just return by value).

float & operator [] (int i) 
{
    return _item[i];
}
float operator [] (int i) const 
{
    return _item[i];
}
我为君王 2024-12-25 03:01:52

这是很正常的——你创建一个运算符,它既提供常规值的引用,也提供常量值的引用;

float & operator [] (int i) 
{
    return _item[i];
}

const float & operator [] (int i) const 
{
    return _item[i];
}

此模式适用于原子类型(例如 float、int 等)以及更复杂的结构和类类型。

This is quite normal -- you make an operator which provides both at regular value by reference as well as a const value by reference;

float & operator [] (int i) 
{
    return _item[i];
}

const float & operator [] (int i) const 
{
    return _item[i];
}

This pattern works for both atomic types such as float,int, etc.., as well as more complex struct and class types.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文