Board index » cppbuilder » non-const function called for const object

non-const function called for const object


2005-12-20 08:04:07 AM
cppbuilder1
This is my first attempt at a copy constructor and I'm not quite sure
why I'm getting this warning with the following code...
__fastcall Format::Format(const ::Format &format)
{
setID(format.getID()); <-- warning occurs here
setDescription(format.getDescription()); <-- and here
}
const int __fastcall Format::getID()
{
return id;
}
void __fastcall Format::setID(const int value)
{
id = value;
}
Any ideas?
TIA
 
 

Re:non-const function called for const object

M < XXXX@XXXXX.COM >wrote:
Quote
This is my first attempt at a copy constructor and I'm not quite sure
why I'm getting this warning with the following code...

__fastcall Format::Format(const ::Format &format)
{
setID(format.getID()); <-- warning occurs here
setDescription(format.getDescription()); <-- and here
}
You need to make the following function const.
Quote
const int __fastcall Format::getID()
{
return id;
}
int Format::getID() const
{
return id;
}
The 'const' _after_ the function's parameter brackets declares the
function to not modify the object on which it is called.
Oh, and the 'const' applied to the return value and the __fastcall are
cruft you don't need.
Alan Bellingham
--
Team Thai Kingdom
<url:www.borland.com/newsgroups/>Borland newsgroup descriptions
<url:www.borland.com/newsgroups/netiquette.html>netiquette
 

Re:non-const function called for const object

"Alan Bellingham" < XXXX@XXXXX.COM >wrote in message
Quote
You need to make the following function const.
And to getDescription() as well.
Gambit
 

{smallsort}

Re:non-const function called for const object

Alan Bellingham wrote:
Quote
int Format::getID() const
{
return id;
}

The 'const' _after_ the function's parameter brackets declares the
function to not modify the object on which it is called.

Oh, and the 'const' applied to the return value and the __fastcall are
cruft you don't need.

Alan Bellingham
Thanks
 

Re:non-const function called for const object

"Remy Lebeau \(TeamB\)" < XXXX@XXXXX.COM >wrote:
Quote
And to getDescription() as well.
Indeed. And probably a whole lot of others. I thought I'd restrict
myself to the function he'd actually shown :-)
Alan Bellingham
--
ACCU Conference 2006 - 19-22 April, Randolph Hotel, Oxford, UK