Is it safe for me to delete one of those two functions with the same name 'getTime'

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
 
X

Xiangliang Meng

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?

If I find some function call Clock::getTime(), how can I know whether it
will invoke the first one or the second one?

Best Regards,

Xiangliang Meng
 
S

Sharad Kala

Xiangliang Meng 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?
If I find some function call Clock::getTime(), how can I know whether it
will invoke the first one or the second one?

Think about it, how would const Clock objects invoke getTime() ? For that
matter see which version gets invoked for them.
-Sharad
 
D

Daniel T.

Xiangliang Meng 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?

Yes and no. Remove the non-const version. The only time you need both a
const and non-const version like this is if the non-const version
returns a modifiable reference to a member-variable of the class. In
this case the function doesn't do that.
If I find some function call Clock::getTime(), how can I know whether it
will invoke the first one or the second one?

If the Clock object in question is const, the const version of getTime
will be called...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,185
Members
46,736
Latest member
AdolphBig6

Latest Threads

Top