Now I have a constant instance of this struct called 'TEST'. I tried to call the method 'check' with it, but it gives me an error:
1 2 3
const Bounds TEST = {3, 14};
bool b = TEST.check(6);
error C2662: 'Bounds::check' : cannot convert 'this' pointer from 'const Bounds' to 'Bounds &'.
My guess is that I have to ensure that the method 'check' does not change the values of 'min' and 'max' in order to use this method on a constant instance. Unfortunately I don't know if this is possible and how it is done.
Could somebody give me a hint?
Thanks a lot for your help.
You can only call functions that have been declared as 'const' on a const object:
1 2 3 4 5 6 7 8 9 10 11 12 13
struct Bounds
{
int min;
int max;
// function does not change the member variable
// so it should be declared const.
bool check(int len) const;
};
bool Bounds::check(int len) const // here too
{
return ((len <= max) && (len >= min));
}