Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c++ > #86849

Re: Virtual functions: To const, or not to const?

From Juha Nieminen <nospam@thanks.invalid>
Newsgroups comp.lang.c++
Subject Re: Virtual functions: To const, or not to const?
Date 2022-10-10 06:20 +0000
Organization Aioe.org NNTP Server
Message-ID <ti0djb$1ghm$1@gioia.aioe.org> (permalink)
References <thosit$6fp$1@gioia.aioe.org> <thp091$3lkbh$1@dont-email.me>

Show all headers | View raw


David Brown <david.brown@hesbynett.no> wrote:
> Shouldn't it be a matter of deciding whether the function logically 
> changes the object or not?  If it clearly changes the object, it must be 
> non-const.  But if it leaves the object with the same publicly visible 
> state, make it const.  Any later bits that might need changing should be 
> mutable.
> 
> A virtual function is an interface, not implementation detail.  It 
> doesn't make sense to me to have a virtual function that is logically 
> const in a base class, but logically non-const in a derived class.

There are situations where the base class function is *so* abstract that
it doesn't even express if the derived class implementations should be
mutable or const. Some derived classes might want to implement as a
non-const function, other derived classes as a const function (so that
it can be called with const objects/references).

There's really no way to allow for this in the base class. Making the
base class function const (and expecting the derived class to use
const cast to eg. call other of its own non-const functions) seems
a bit wrong. On the other hand, making it non-const now makes calling
the function with a const object harder. Declaring both a non-const
and const versions of the function may burden the derived class with
having to implement a const version that it can't logically implement.

All possible solutions have something wrong about them.

Back to comp.lang.c++ | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


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