473,385 Members | 2,044 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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

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
Jul 22 '05 #1
3 1462

"Xiangliang Meng" <xi*************@hotmail.com> wrote in message
news:ch**********@zcars0v6.ca.nortel.com...
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

Jul 22 '05 #2
Xiangliang Meng wrote:

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
Jul 22 '05 #3
"Xiangliang Meng" <xi*************@hotmail.com> wrote:
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...
Jul 22 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Richard Hollenbeck | last post by:
I'm trying to write some code that will convert any of the most popular standard date formats twice in to something like "dd Mmm yyyy" (i.e. 08 Jan 1908) and compare the first with the second and...
10
by: solosnake | last post by:
Whilst browsing Flipcode I noticed this code: class CConsole: public I_TextOutput { public: ... void Release() { delete this; } ... };
16
by: Philip Boonzaaier | last post by:
I want to be able to generate SQL statements that will go through a list of data, effectively row by row, enquire on the database if this exists in the selected table- If it exists, then the colums...
6
by: Tim Greenwood | last post by:
I am at my wits end here. I have a simple project with one global function returning a long value. __declspec(dllexport) long myfunc() { } I've built the dll, added it as a reference to my...
6
by: jojowebdev | last post by:
Do javascript cookies REALLY have to be this hard? function setCookie( name, value, expires, path, domain, secure ) { var today = new Date(); today.setTime( today.getTime() ); if ( expires ) {...
22
by: Cylix | last post by:
I have a 4row x 1col table, I would like to drop all the content of row three. Since Mac IE5.2 does not suppport deleteRow method, I have also try to set the innerHTML=''; but it does not work. ...
3
by: codeman | last post by:
Hi all Lets say we have two tables: Customer: Customer_number : Decimal(15) Name : Char(30) Purchase: Purchase_number : Decimal(15)
1
by: johnlim20088 | last post by:
Hi, Currently I have 6 web projects located in Visual Source Safe 6.0, as usual, everytime I will open solution file located in my local computer, connected to source safe, then check out/check in...
95
by: hstagni | last post by:
Where can I find a library to created text-based windows applications? Im looking for a library that can make windows and buttons inside console.. Many old apps were make like this, i guess ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.