Advice for using "const" keyword in C ++

I am writing classes for Vector3 and Quaternion. Here is my code:

// .h file
Quaternion operator * (const Vector3& v) const;

// .cpp file
Quaternion Quaternion::operator * (const Vector3& v) const
{
    float s = -(m_v.dot(v));
    Vector3 vt = (v*m_s) + m_v.cross(v);
    return Quaternion(s, vt.getX(), vt.getY(), vt.getZ());
}

      

I got errors with the "return" line because I declared inside Vector3.h like this:

float&  getX();
float&  getY();
float&  getZ();

      

I figured out that I could convey this case by declaring here:

const float&    getX() const;
const float&    getY() const;
const float&    getZ() const;

      

I also saw that I won't be using this anymore:

Vector3 v(1.0f, 2.0f, 3.0f);
v.getX()++;
// or v.getX() += 1; => coz I feel writing code like this is more readable.

      

And I should write code like this:

float x = v.getX();  // I dont like this coz it will waste memory
                     // if it not an "float" but a big object
x += 1;
v.setX(x);

      

So my questions are:

  • Is there a way to satisfy both of these cases, or is it just a trade-off between choices?
  • Is it good practice for a C ++ programmer to use the "const" keyword frequently?
+3


source to share


3 answers


What you can do is provide two different versions of the function.

class Vector {
    public:
        float getX() const;
        float & getX();
};

void foo(const Vector & const_v, Vector & v) {
    v.getX() += 1;
    const_v.getX() += 1; // Won't work.
}

      



Note that if yours Vector

is just a container for data, it's easier to just declare it struct

and allow direct access to all members. You should only use getters and setters when there is a non-zero chance that access to private fields should be regulated in some way (for example, you want a to Vector

continually normalize, so any entry in the field should change all of them to very specific way).

+4


source


First of all, in general, you should mark everything as const

if possible. This will save you a lot of debugging or documentation time because the compiler can help diagnose logic problems.

To return references to private members, the C ++ standard library uses a two-versioning approach, one declared const

and the other not. This could be your approach too.



But in your specific case, you can just make it public. Many books tell you to always declare members as private and use getters and setters because this is how "OOP" works, but it is not. If the class simply stores data and does not provide any other abstraction, then there is nothing that can be done by making these fields private.

Finally, if you are deeply concerned about efficiency, you should return float

when you don't really need it float&

. The latter also requires the allocation of memory or a register, possibly more than the memory assumed simple float

. Access float&

also requires indirection, which is quite costly.

+1


source


It is good programming practice to use const

get in a function because the get function should only have the privilege to read data, not modify data.

-1


source







All Articles