D
Denis Remezov
Xiangliang said:Hi, all
class Clock
{
Clock() { Time_m = 0;}
void setTime(unsigned int time = 0)
{
Time_m = time;
}
unsigned int getTime() const
{
return Time_m;
}
unsigned int getTime()
{
return Time_m;
}
private:
unsigned int Time_m;
};
Is it safe for me to delete one of those two functions with the same name
'getTime'? Are there 'big' differences between those two funtions for users
of this class?
Get rid of the non-const version. Why did you need it in this particular
form?
There is a substantial difference: you cannot call non-const member functions
through a const reference or pointer. Meanwhile, you should be using const
references/pointers whenever logical.
If I find some function call Clock::getTime(), how can I know whether it
will invoke the first one or the second one?
If you have a const reference then, obviously, getTime() const will be called.
If you are calling through a non-const reference, the non-const version of
getTime() will be used if it exists; if it doesn't exist, getTime() const
will be used.
Denis