Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c++ > #12665
| From | yatremblay@bel1lin202.(none) (Yannick Tremblay) |
|---|---|
| Newsgroups | comp.lang.c++ |
| Subject | Re: would you consider this function const? |
| Date | 2011-12-19 18:16 +0000 |
| Organization | A noiseless patient Spider |
| Message-ID | <jcnv2j$9qa$1@dont-email.me> (permalink) |
| References | <jcaem0$gac$1@speranza.aioe.org> <jcai9t$ee9$1@dont-email.me> |
In article <jcai9t$ee9$1@dont-email.me>,
Victor Bazarov <v.bazarov@comcast.invalid> wrote:
>
>IME 'const_cast' is good only for one thing: to add const where one
>needs an overload resolution to go a particular way. Any other use of
>'const_cast' is an instance of abuse. That's just IME, of course.
I would tolerate const_cast inside a method when conceptually, a
method is const (i.e. does not conceptually change the object) but due
to implementation details, the object internal needs to be modified.
I very rarely see a reason to use it. But for example, some peoples
are fans of delaying initialisation using a pattern like:
class A
{
public:
A(): m_initDone(false), ... {};
std::string foo() const
{
if(!m_initDone)
{
const_cast<A *>(this)->init();
}
// do the work
}
private:
void init() {...};
bool m_initDone;
// other member variables
}
I am not a big fan of the above but this would be an example where
const_cast allow an object to modify its internals without breaking
the concept that the object has not changed.
Unfortunately, most of the time const_cast is used to cast-away
constness from code that is const-correct to interface with
const-incorrect code when the correct thing to do would be fix the
const-incorrect code :-( (Actually, most of the time I see it, C-style
hard casts are being used which is even worse).
Yannick
Back to comp.lang.c++ | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
would you consider this function const? john <gh14tq5@yahoo.com> - 2011-12-14 10:17 -0500
Re: would you consider this function const? Victor Bazarov <v.bazarov@comcast.invalid> - 2011-12-14 11:19 -0500
Re: would you consider this function const? john <gh14tq5@yahoo.com> - 2011-12-15 08:10 -0500
Re: would you consider this function const? Victor Bazarov <v.bazarov@comcast.invalid> - 2011-12-15 08:21 -0500
Re: would you consider this function const? john <gh14tq5@yahoo.com> - 2011-12-15 08:44 -0500
Re: would you consider this function const? Leigh Johnston <leigh@i42.co.uk> - 2011-12-15 14:07 +0000
Re: would you consider this function const? Leigh Johnston <leigh@i42.co.uk> - 2011-12-15 14:14 +0000
Re: would you consider this function const? Victor Bazarov <v.bazarov@comcast.invalid> - 2011-12-15 09:11 -0500
Re: would you consider this function const? john <gh14tq5@yahoo.com> - 2011-12-15 09:30 -0500
Re: would you consider this function const? Victor Bazarov <v.bazarov@comcast.invalid> - 2011-12-15 10:06 -0500
Re: would you consider this function const? john <gh14tq5@yahoo.com> - 2011-12-15 12:42 -0500
Re: would you consider this function const? Victor Bazarov <v.bazarov@comcast.invalid> - 2011-12-15 13:32 -0500
Re: would you consider this function const? yatremblay@bel1lin202.(none) (Yannick Tremblay) - 2011-12-19 18:16 +0000
Re: would you consider this function const? Tobias Müller <troplin@bluewin.ch> - 2011-12-19 19:05 +0000
Re: would you consider this function const? Joshua Maurice <joshuamaurice@gmail.com> - 2011-12-19 14:36 -0800
Re: would you consider this function const? Tobias Müller <troplin@bluewin.ch> - 2011-12-20 06:28 +0000
Re: would you consider this function const? Joshua Maurice <joshuamaurice@gmail.com> - 2011-12-19 23:48 -0800
csiph-web