Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c++ > #86825
| Path | csiph.com!aioe.org!NK0c7qMEn6mmBWqphs27pg.user.46.165.242.75.POSTED!not-for-mail |
|---|---|
| From | Juha Nieminen <nospam@thanks.invalid> |
| Newsgroups | comp.lang.c++ |
| Subject | Virtual functions: To const, or not to const? |
| Date | Fri, 7 Oct 2022 09:47:11 -0000 (UTC) |
| Organization | Aioe.org NNTP Server |
| Message-ID | <thosit$6fp$1@gioia.aioe.org> (permalink) |
| Injection-Info | gioia.aioe.org; logging-data="6649"; posting-host="NK0c7qMEn6mmBWqphs27pg.user.gioia.aioe.org"; mail-complaints-to="abuse@aioe.org"; |
| User-Agent | tin/2.4.5-20201224 ("Glen Albyn") (Linux/5.15.69-grsec-kapsi (x86_64)) |
| X-Notice | Filtered by postfilter v. 0.9.2 |
| Xref | csiph.com comp.lang.c++:86825 |
Show key headers only | View raw
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). The answer seems simple enough: Obviously it's better to not make it const, to allow for this possibility. Problem is, when it's not const, now it can't be called using a const instance, or const reference or pointer to one. There may be situations where the only thing you have is a const version of the object, and there's no way around that. (Ok, technically speaking you *can* call it, by using const_cast. But that's a really ugly solution, bad design, and in some cases might even potentially break something. (Const code might assume the object doesn't change.)) "Well, in that case offer *both* versions of the virtual function in the base class, duh! Problem solved!" 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. Sometimes a const version just doesn't make any sense. (For example, a const version of std::vector::push_back() wouldn't make much sense. What would it be supposed to do? Nothing? That would make it really odd design, and would have the potential to break something.) This isn't even a question of "const correctness". It's a question of getting a compiler error if all you have is a const reference (and no way around that) and the virtual function happens to be non-const (even though the implementation doesn't actually change anything).
Back to comp.lang.c++ | Previous | Next — Next 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