MESSAGE
DATE | 2008-09-03 |
FROM | Ruben Safir
|
SUBJECT | Subject: [NYLXS - HANGOUT] C++ Workshop Const Functions
|
Date: Sun, 31 Aug 2008 02:28:47 -0500 From: Paavo Helde Lines: 61 Message-ID: NNTP-Posting-Date: Sun, 31 Aug 2008 02:28:47 -0500 Newsgroups: comp.lang.c++ Path: reader1.panix.com!panix!bloom-beacon.mit.edu!newsswitch.lcs.mit.edu!newsfeed.cwix.com!dc3peer2.nntp.savvis.net!peer.nntp.savvis.net!cycny02.gnilink.net!cyclone1.gnilink.net!gnilink.net!nx01.iad01.newshosting.com!newshosting.com!216.196.98.140.MISMATCH!Xl.tags.giganews.com!border1.nntp.dca.giganews.com!nntp.giganews.com!local02.nntp.dca.giganews.com!news.giganews.com.POSTED!not-for-mail References:
Subject: Re: error: passing `const Weight' as `this' argument of `float Weight::wgt()' discards qualifiers User-Agent: Xnews/5.04.25 X-Abuse-and-DMCA-Info: Please be sure to forward a copy of ALL headers X-Abuse-and-DMCA-Info: Otherwise we will be unable to process your complaint properly X-Complaints-To: abuse-at-giganews.com X-DMCA-Notifications: http://www.giganews.com/info/dmca.html X-Postfilter: 1.3.39 X-Trace: sv3-zSNH/CfU/RfOVKi6IK9qk51Y6F54YSOTRXqItMKO02dvVAoMeeeBQWpqroTC/2K3Wc8R77P6irb4kMd!dwiE/BMzw9Bb9OCwBI6nrMTLMlwlglOTNgk5Dy0sS5a1LZ042vB4rt5576MFUFxj38Ake7Vz X-Usenet-Provider: http://www.giganews.com Xref: panix comp.lang.c++:1019775 MIME-Version: 1.0 Content-Type: text/plain
Ruben kirjutas:
> On Sat, 30 Aug 2008 01:39:48 -0500, Paavo Helde wrote: > >>> Weight& Weight::operator=(const Weight &fweight) { >>> wgt_ = fweight.wgt(); >> >> And here you call it through a const Weight reference. >> >> Make your getter methods const member functions. > > what does making the fuction const do practically? If I make a const > function that assings some member a value, will it compile?
No, it will not, and that's the point of the const system. It holds your hand and tries to prevent you shoot in your leg.
Some people argue that constness it not the only or most important aspect to keep in mind when working with objects, and there should be no obligation to use it. However, it has been built into the language (in signatures of assignment op and copy ctor and the rule about binding temporaries to const references) as well as into the standard library, so practically there is no alternative to making all code const-correct from the start.
Here, the standard signature for assignment op is as you wrote:
Weight& Weight::operator=(const Weight &fweight);
The constness of the argument now propagates further to every use of the fweight reference. I try to explain the propagation mechanism in more detail, feel free to use your books instead if I am not making any sense ;-)
The signature promises that inside this function the fweight argument is kept const. Now you are calling function fweight.wgt() which is non-const and thus may potentially modify the object referenced by fweight. Thus the compiler cannot be sure it can keep up with the promises of operator= () signature, so the compilation is aborted.
Now, if you make wgt() const,
class Weight { public: float wgt() const { return wgt_; };
Now the operator=() is happy and compiles. OTOH, if you had called some non-const member function on the same object from wgt(), you would have got another error there, i.e. the error will also "propagate". Now you could add 'const' to this next member function and see if the thing compiles, etc.
However, making the code const-correct this way is quite tedious, better is to decide up-front which member functions should not modify the object, and mark them 'const' from the start. The "getter" functions like wgt() are the obvious candidates.
hth Paavo
-- http://www.mrbrklyn.com - Interesting Stuff http://www.nylxs.com - Leadership Development in Free Software
So many immigrant groups have swept through our town that Brooklyn, like Atlantis, reaches mythological proportions in the mind of the world - RI Safir 1998
http://fairuse.nylxs.com DRM is THEFT - We are the STAKEHOLDERS - RI Safir 2002
"Yeah - I write Free Software...so SUE ME"
"The tremendous problem we face is that we are becoming sharecroppers to our own cultural heritage -- we need the ability to participate in our own society."
"> I'm an engineer. I choose the best tool for the job, politics be damned.< You must be a stupid engineer then, because politcs and technology have been attached at the hip since the 1st dynasty in Ancient Egypt. I guess you missed that one."
© Copyright for the Digital Millennium
|
|