BorlandTalk.com Forum Index BorlandTalk.com
Borland discussion newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

non-const function called for const object

 
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> C++ Builder (Language C++)
View previous topic :: View next topic  
Author Message
M
Guest





PostPosted: Tue Dec 20, 2005 12:04 am    Post subject: non-const function called for const object Reply with 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
}

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





PostPosted: Tue Dec 20, 2005 12:16 am    Post subject: Re: non-const function called for const object Reply with quote



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





PostPosted: Tue Dec 20, 2005 12:30 am    Post subject: Re: non-const function called for const object Reply with quote




"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





PostPosted: Tue Dec 20, 2005 12:41 am    Post subject: Re: non-const function called for const object Reply with quote

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





PostPosted: Tue Dec 20, 2005 10:13 am    Post subject: Re: non-const function called for const object Reply with quote

"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
Display posts from previous:   
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> C++ Builder (Language C++) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.