Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c++ > #86841
| From | Manfred <noname@add.invalid> |
|---|---|
| Newsgroups | comp.lang.c++ |
| Subject | Re: Virtual functions: To const, or not to const? |
| Date | 2022-10-08 21:42 +0200 |
| Organization | Aioe.org NNTP Server |
| Message-ID | <thsjs1$1d02$1@gioia.aioe.org> (permalink) |
| References | <thosit$6fp$1@gioia.aioe.org> |
On 10/7/2022 11:47 AM, Juha Nieminen wrote: > I have encountered this small dilemma several times. It's a question of > const correctness and proper OO design in C++. > > Should a virtual function in a base class be const or not? This especially > in cases where it's very possible that a derived implementation wants > to change its own state (and thus would want the function to not be > const). As others have said, 'const'-ness is not really correlated with the function being virtual or not; rather, it is a design choice strictly coupled with the expected behavior (or contract) of the function. In short, if it changes the (logical) state of the object, then it's just non-const; if it doesn't, and it is not meant to change it, then it may be (and probably is best, if you are convinced of your design) qualified as const. I'll add that during a course on C++ design, more than two decades ago, the guy stressed that 'const is a very strong qualifier' in the C++ type system. Emphasis on /strong/, meaning that the decision whether some member is or is not const should be considered thoroughly in C++ design, despite how tiny the change is in code. If, later along the way, you find yourself in the need of changing that design choice, then I'd either make a new function or at least make an overload (which, in C++, works well given the 'strong' nature of the qualifier) <snip> > Except that sometimes, in some situations, there may not be any > reasonable way to implement a const version of a function, because > it *has to* change the object in order to function correctly. I see here one of two things happening: either the 'state' of the object or the semantics of the function is not defined properly. A third, rare IME, alternative is that you would have some data member that is not really part of the logical state of the object - that's the use for 'mutable'.
Back to comp.lang.c++ | Previous | Next — Previous in thread | Find similar | Unroll thread
Virtual functions: To const, or not to const? Juha Nieminen <nospam@thanks.invalid> - 2022-10-07 09:47 +0000
Re: Virtual functions: To const, or not to const? David Brown <david.brown@hesbynett.no> - 2022-10-07 12:50 +0200
Re: Virtual functions: To const, or not to const? Juha Nieminen <nospam@thanks.invalid> - 2022-10-10 06:20 +0000
Re: Virtual functions: To const, or not to const? Richard Damon <Richard@Damon-Family.org> - 2022-10-10 07:38 -0400
Re: Virtual functions: To const, or not to const? Juha Nieminen <nospam@thanks.invalid> - 2022-10-10 12:08 +0000
Re: Virtual functions: To const, or not to const? Richard Damon <Richard@Damon-Family.org> - 2022-10-10 20:47 -0400
Re: Virtual functions: To const, or not to const? Paavo Helde <eesnimi@osa.pri.ee> - 2022-10-10 15:13 +0300
Re: Virtual functions: To const, or not to const? Tim Rentsch <tr.17687@z991.linuxsc.com> - 2022-10-10 09:43 -0700
Re: Virtual functions: To const, or not to const? Tim Rentsch <tr.17687@z991.linuxsc.com> - 2022-10-10 09:26 -0700
Re: Virtual functions: To const, or not to const? Paavo Helde <eesnimi@osa.pri.ee> - 2022-10-07 14:00 +0300
Re: Virtual functions: To const, or not to const? Richard Damon <Richard@Damon-Family.org> - 2022-10-07 07:29 -0400
Re: Virtual functions: To const, or not to const? Öö Tiib <ootiib@hot.ee> - 2022-10-07 05:55 -0700
Re: Virtual functions: To const, or not to const? "Alf P. Steinbach" <alf.p.steinbach@gmail.com> - 2022-10-08 14:56 +0200
Re: Virtual functions: To const, or not to const? Bonita Montero <Bonita.Montero@gmail.com> - 2022-10-08 16:35 +0200
Re: Virtual functions: To const, or not to const? Michael S <already5chosen@yahoo.com> - 2022-10-08 11:03 -0700
Re: Virtual functions: To const, or not to const? Bonita Montero <Bonita.Montero@gmail.com> - 2022-10-08 20:36 +0200
Re: Virtual functions: To const, or not to const? Mr Flibble <flibble@reddwarf.jmc.corp> - 2022-10-08 20:19 +0100
Re: Virtual functions: To const, or not to const? Bonita Montero <Bonita.Montero@gmail.com> - 2022-10-10 17:58 +0200
Re: Virtual functions: To const, or not to const? Muttley@dastardlyhq.com - 2022-10-09 09:19 +0000
Re: Virtual functions: To const, or not to const? Manfred <noname@add.invalid> - 2022-10-08 21:42 +0200
csiph-web