0

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

};
2
  • 2
    I think there's a fundamental misunderstanding taking place at the core of this question. Nov 27, 2011 at 1:43
  • You do not need to Modify all my operators to const. Half of them would make no sense on a const object anyway. Nov 27, 2011 at 1:44

2 Answers 2

1

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];
}
6
  • Problem here is that you don't return the float by reference in the const case -- which changes the semantics of what is going on.
    – Soren
    Nov 27, 2011 at 1:50
  • @Soren: Not in the posted use cases. Only if you took an explicit const reference- very unusual for float values- would it actually change that.
    – Puppy
    Nov 27, 2011 at 1:59
  • I strongly feel that 1) Changing the const-ness of variable should not affect program semantic, only whether it is well-formed and 2) removing const should not make code ill-formed. This design breaks 1). In particular, the precondition of const and non-const [] must be the same. (Was not true with C++97 string.) The invalidation semantic must also be the same (not true for begin/end/[] with C++97 string: the first non-const begin/end/[] call would invalidate const_iterator obtained before).
    – curiousguy
    Nov 27, 2011 at 5:38
  • (...) With your design, removing const can change program semantic and silently break code, and so can adding const. x[i] should always be a lvalue, or always be a rvalue. This should not be one or the other depending on const-ness. lvalue-ness changes the semantic of reference binding.
    – curiousguy
    Nov 27, 2011 at 5:40
  • @DeadMG "Only if you took an explicit const reference- very unusual for float values- would it actually change that." it is not unusual for generic code to take const references to some object type
    – curiousguy
    Nov 27, 2011 at 5:49
0

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.

2
  • 1
    There's nothing "atomic" about float or int.
    – Puppy
    Nov 27, 2011 at 2:03
  • "atomic types" Do you mean fundamental types? non-aggregate types?
    – curiousguy
    Nov 27, 2011 at 5:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.