|
From: seanf on 30 Sep 2006 16:46 Hi group Should I ever declare pure virtual functions const in an ABC? Whether or not implementations change their state seems to be none of the interface's business. Also, if I get it wrong, I can't un-const a function in future without perhaps breaking existing callers. On the other hand, if I don't, I can end up with embarrassingly non-const getName() and the like in an otherwise const-correct implementation class. Ideas appreciated Sean -- temporary email: cppthrowaway at blueyonder dot co dot uk [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: Frederick Gotham on 30 Sep 2006 18:06 seanf posted: > Hi group > > Should I ever declare pure virtual functions const in an ABC? No, the latin alphabet is notorious for ignoring the "virual" directive. I would suggest you use the Greek alphabet. > Whether or not implementations change their state seems to be none of > the interface's business. Depends if the interface is running a non-profit business. > Also, if I get it wrong, I can't un-const a function in future without > perhaps breaking existing callers. Good point, you should ask any callees if you can put them on hold before making alterations. > On the other hand, if I don't, I can end up with embarrassingly > non-const getName() and the like in an otherwise const-correct > implementation class. Yes, this is the eternal paradox. -- Frederick Gotham -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: Thomas J. Gritzan on 1 Oct 2006 05:47 Frederick Gotham schrieb: > seanf posted: > >> Hi group >> >> Should I ever declare pure virtual functions const in an ABC? > > > No, the latin alphabet is notorious for ignoring the "virual" directive. I > would suggest you use the Greek alphabet. The Greek alphabet is a POD (at least it is plain old), so it shouldn't have virtual functions at all. > seanf posted: >> Whether or not implementations change their state seems to be none of >> the interface's business. But it is. A clone() or a size() function doesn't change the (visible) state. So you should be able to call them on const references. Make them const functions. When you need to change some member variables for whatever reason (that don't affect the user-visible state), you can make the variables mutable. -- Thomas http://www.netmeister.org/news/learn2quote.html [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: Alberto Ganesh Barbati on 1 Oct 2006 05:57 seanf ha scritto: > Hi group > > Should I ever declare pure virtual functions const in an ABC? > > Whether or not implementations change their state seems to be none of > the interface's business. Also, if I get it wrong, I can't un-const a > function in future without perhaps breaking existing callers. You can't un-const a function, but you can use mutable members. > On the other hand, if I don't, I can end up with embarrassingly > non-const getName() and the like in an otherwise const-correct > implementation class. > > Ideas appreciated If the function is not expected to alter the state (at least the state that is observable through the interface), for example a getter, then I see nothing wrong in declaring it const. In the unlikely event that implementation really needs to alter its (non-observable) state despite the const contract, it can still use mutable members. The need to use of mutable members should be inconvenient enough to discourage programmers from abusing the contract, yet it's a small price to pay IMHO in front of const-correctness. Just my 2 eurocent, Ganesh -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: kwikius on 1 Oct 2006 05:55
Frederick Gotham wrote: > seanf posted: > > > Hi group > > > > Should I ever declare pure virtual functions const in an ABC? > > > No, the latin alphabet is notorious for ignoring the "virual" directive. I > would suggest you use the Greek alphabet. As a C++ newbie, the term perhaps has only an alphabetic meaning to you, but it's well recognised within the context of the C++ language. regards Andy Little { Additional moderator's info: for those unfamiliar with the term "ABC", see <http://www.parashift.com/c++-faq-lite/abcs.html#faq-22.3>. -mod } -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ] |