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 method like this:
const bool isReady() {
return ready;
}
But I've seen that some IDE autogenerate getter in this way:
bool getReady() const {
return ready;
}
But, writing delegates, it happened to me to find this error if the const
is after the function:
member function 'isReady' not viable: 'this' argument has type 'const VideoReader', but function is not marked const
So that, what is the better way to write a const getter? Do I really have to care about?
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.
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 aa 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.
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.
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
.
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.