C++ Logo

std-proposals

Advanced search

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

From: Frederick Virchanza Gotham <cauldwell.thomas_at_[hidden]>
Date: Tue, 12 Apr 2022 22:11:38 +0100
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.

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.

Received on 2022-04-12 21:11:40