C++ Logo

std-proposals

Advanced search

Re: [std-proposals] Derived class's function invokes base class's function

From: Marcin Jaczewski <marcinjaczewski86_at_[hidden]>
Date: Wed, 13 Apr 2022 01:14:33 +0200
wt., 12 kwi 2022 o 23:11 Frederick Virchanza Gotham via Std-Proposals
<std-proposals_at_[hidden]> napisaƂ(a):
>
> On 4/11/22, Marcin Jaczewski wrote:
> >
> > Not to mention that if we would like to call diffrent lasers by the
> > same function we can have a base virtual class
> > `ILaser` that will be a common interface for all `Laser<>`.
>
>
> I'm using my imagination here to figure out what you mean, I'm
> thinking something like:
>
> struct ILaser {
>
> virtual void Trigger(void) = 0;
>
> };
>
> template<typename... Modifiers>
> struct Laser : ILaser {
>
> std::tuple<Modifiers...> _mod; //could be base classes
>
> void Trigger(void) override
> {
> (get<Modifiers>(_mod).apply(), ...);
> }
>
> };
>
> In case my imagination is inaccurate, could you please give me a full
> compilable example of what you meant. Later I can copy-paste it into
> my PDF document and compare/contrast its merits to my proposal.
>
Yes, this was exactly what I intended, tomorrow if I find some free
time I will try
prepare a full example for this.

> Another similar strategy to what you proposed (if my imagination's
> right) would be to keep an array of function pointers, something like:
>
> class Laser {
> protected:
>
> void (*supplements_to_Trigger[16u])(void) = {}; // starts off as
> all nullptr's
>
> bool Supplement_Trigger(void (*const p)(void))
> {
> for ( auto &e : supplements_to_Trigger )
> {
> if ( nullptr == e )
> {
> e = p;
> return true;
> }
> }
>
> return false;
> }
>
> void Trigger(void)
> {
> // pre-processing goes here
>
> for ( auto const &e : supplements_to_Trigger )
> {
> if ( nullptr == e ) break;
>
> e();
> }
> }
> };
>
>
> And so then the derived class would call "Supplement_Trigger"
> immediately after constructing the base object.
>
> I'd rather just write 'continue' after the method.
Array of function pointers was not the solution I would suggest.
Probably too big overhead (fixed min size) and too restricted (fixed max size).
Probably some CRTP could improve this.

Btw for example for you, who really should decide what function to call?
Sometimes the base class knows if it should participate in some
operation but in some other
cases the final class only knows who should do it.
Or simply you control the whole hierarchy and deciding class is
irrelevant as you can alter both.

> --
> Std-Proposals mailing list
> Std-Proposals_at_[hidden]
> https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals

Received on 2022-04-12 23:14:46