Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c++ > #86827
| From | Paavo Helde <eesnimi@osa.pri.ee> |
|---|---|
| Newsgroups | comp.lang.c++ |
| Subject | Re: Virtual functions: To const, or not to const? |
| Date | 2022-10-07 14:00 +0300 |
| Organization | A noiseless patient Spider |
| Message-ID | <thp0rl$3lmsb$2@dont-email.me> (permalink) |
| References | <thosit$6fp$1@gioia.aioe.org> |
07.10.2022 12:47 Juha Nieminen kirjutas: > 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). > IMO, the 'const' should be applied logically. There is a virtual function which does something. Is this something that conceptually should not modify the object state? If yes, then make the virtual function const. If a derived class overrides this function, it should conceptually still do something which does not modify the object. If it conceptually does, then it's possible you have bigger problems with the design, starting with the function name. If the derived class modifies its state only technically, e.g. by caching something, we have means to express this in C++ cleanly - that's the 'mutable' keyword. When following these rules, now if you have a const reference to the object and need to call a non-const virtual function on it, this means you want to call a function on a const object which might (again, conceptually) modify the object state. That's a design smell, and this is the exact reason why the language has the const qualifier in the first place - to catch such things at compile time so the design could be fixed. In reality the things won't work out so nicely. In some cases the base class virtual interface is at so abstract level it is impossible to say if the functions might conceptually modify the object or not. According to the above guidelines these virtual functions should be non-const, which in turn means that this class cannot be really used via const references. In my code I have got several "non-const" classes which are basically always accessed only over non-const references. Yes, this looses the benefits of 'const', but at least it is honest about that and does not hide it behind a wall of const_cast-s. Other scenario is e.g. with virtual functions which take callbacks or functors as arguments. Some callbacks might want to modify the object, some not. In that case one should provide two different virtual functions, one const and the other non-const. If there is some override which does not make sense and which should not be called, then it's easy for me. I just put a debug assert failure and a runtime exception there. hth Paavo
Back to comp.lang.c++ | Previous | Next — Previous in thread | 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