C++ const in getter

前端 未结 5 1357
鱼传尺愫
鱼传尺愫 2021-01-30 22:55

I\'m still learning about C++ and I\'m reading everywhere that I have to use const everywhere I can (for speed reason I think).

I\'m usually write my getter

相关标签:
5条回答
  • 2021-01-30 23:38

    const method informs compiler that you will not modify class instance on which this method is called:

    class A {
    public:
    bool getReady() const {
        return ready;
    }
    };
    

    so if you try to modify your object inside getReady() then compiler will issue error. Const methods are usefull where you have ie.: const A&, or const A*, then you can call only const methods on such objects.

    as for:

    const bool isReady() {
        return ready;
    }
    

    this const provides actually no real benefit, because bool is copied while isReady() returns. Such const whould make sense if returned type was a const char* or const A&, in such cases const makes your char string or A class instance immutable.

    0 讨论(0)
  • 2021-01-30 23:42

    A const getter has the signature

    bool getReady() const
    

    The other version isn't a const method, it just returns a const value (which is basically useless).

    Having a const getter allows you to call it on const objects:

    const Object obj;
    obj.getReady();
    

    This is only valid if getReady is marked as const.

    0 讨论(0)
  • 2021-01-30 23:43

    They mean two differnt things:

    const bool isReady() {
        return ready;
    }
    

    This returns a constant bool. Meaning a bool which cannot change value from the time it's been created.

    bool getReady() const { 
        return ready;
    }
    

    This is a a constant function, meaning a function that will not alter any member variables of the class it belongs to. This is the style recommended to use for getters, since their only purpose is to retrieve data and should not modify anything in the process.

    0 讨论(0)
  • 2021-01-30 23:47

    There is a huge difference between the two ways.

    const bool isReady()
    

    The code above will return a const bool, but it does not guarantee that the object will not change its logic state.

    bool isReady() const
    

    This will return a bool, and it guarantees that the logic state of your object will not change. In this case it is not necessary to write const in front of the return type. It makes no sense to return a const bool because it is a copy anyway. So making it const is useless. The second const is needed for const correctness, which is not used for speed reasons but to make your program more reliable and safe.

    0 讨论(0)
  • 2021-01-30 23:57

    There is a difference between using the const keyword for the return type or for the method signature. In the first case the returned value will be a constant value. In the second case the method will be a so-called constant method, which cannot change the representation of the object. On constant objects, only the constant methods are callable.

    0 讨论(0)
提交回复
热议问题