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


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

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

From Tim Rentsch <tr.17687@z991.linuxsc.com>
Newsgroups comp.lang.c++
Subject Re: Virtual functions: To const, or not to const?
Date 2022-10-10 09:43 -0700
Organization A noiseless patient Spider
Message-ID <86zge3hbo1.fsf@linuxsc.com> (permalink)
References <thosit$6fp$1@gioia.aioe.org> <thp091$3lkbh$1@dont-email.me> <ti0djb$1ghm$1@gioia.aioe.org> <ti129g$qa0t$3@dont-email.me>

Show all headers | View raw


Paavo Helde <eesnimi@osa.pri.ee> writes:

> 10.10.2022 09:20 Juha Nieminen kirjutas:
>
>> 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).
>
> If that's really so, then it looks like you have got two different
> class hierarchies or at least two interfaces, not one.

Right.  I was wondering how long it would be before someone would
reach this conclusion.

Here is an outline for a scheme that supplies each of the four
logically distinct possibilities:

    struct super_neither {
      protected:
        unsigned u_;

      public:
        super_neither() : u_(0) {}
        virtual ~super_neither();
    };

    struct super_mutable_only : public virtual super_neither {
        virtual unsigned u()       { return ++u_; }
    };

    struct super_nonmutable_only : public virtual super_neither {
        virtual unsigned u() const { return  1+u_; }
    };

    struct super_both : public super_mutable_only, super_nonmutable_only {
        virtual unsigned u()       { return  super_mutable_only::u();    }
        virtual unsigned u() const { return  super_nonmutable_only::u(); }
    };

Different subclasses should choose whichever of the four given
superclasses that is most appropriate to their individual needs.

Disclaimer:  code intended only to illustrate the approach;  some
details may need changing, depending on particular circumstances.

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