| View previous topic :: View next topic |
| Author |
Message |
M Guest
|
Posted: Tue Dec 20, 2005 12:04 am Post subject: non-const function called for const object |
|
|
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
|
|
| Back to top |
|
 |
Alan Bellingham Guest
|
Posted: Tue Dec 20, 2005 12:16 am Post subject: Re: non-const function called for const object |
|
|
M <user (AT) site (DOT) 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:http://www.borland.com/newsgroups/netiquette.html> netiquette
|
|
| Back to top |
|
 |
Remy Lebeau (TeamB) Guest
|
Posted: Tue Dec 20, 2005 12:30 am Post subject: Re: non-const function called for const object |
|
|
"Alan Bellingham" <alanb (AT) episys (DOT) com> wrote
| Quote: | You need to make the following function const.
|
And to getDescription() as well.
Gambit
|
|
| Back to top |
|
 |
M Guest
|
Posted: Tue Dec 20, 2005 12:41 am Post subject: 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
|
|
| Back to top |
|
 |
Alan Bellingham Guest
|
Posted: Tue Dec 20, 2005 10:13 am Post subject: Re: non-const function called for const object |
|
|
"Remy Lebeau (TeamB)" <no.spam (AT) no (DOT) spam.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
|
|
| Back to top |
|
 |
|