Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
259 views
in Technique[技术] by (71.8m points)

templates - Is it possible to override functionality with a mixin-like pattern in c++

let's say I have the following classes:

class Base {
public:
    virtual int do_something() = 0;
protected:
    virtual int give_intermediate_result(int) = 0;

    int m_some_shared_resources;
};

class Foo : public Base {
public:
    virtual int do_something() override { return give_intermediate_result(1); }
protected:
    virtual int give_intermediate_result(int a) override { return a*2; }
};

// and something like this:
class FooMixinA : public virtual Base {
protected:
    virtual int give_intermediate_result(int) override;
};

class FooMixinB : public virtual Base {
public:
    virtual int do_something() override;
};

What I would like to do is something like this:

Foo<FooMixinA, FooMixinB> myfoo (...);

which basically should override Foo's implementation of do_something with that of FooMixinB and give_intermediate_result with that of FooMixinA. Additionally I want mixins to be able to replace other mixins functionality, i. e. the order of the mixins is important as one mixin may use (f. e. the get_intermediate_result of) another mixin. At the same time they should all have access to some shared underlying resource.

Can I do something like that and if so how would I approach it?

question from:https://stackoverflow.com/questions/66045268/is-it-possible-to-override-functionality-with-a-mixin-like-pattern-in-c

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
template<class Base, template<class>class...Mixins>
struct Foo:Base{};
template<class Base, template<class>class M0, template<class>class...Mixins>
struct Foo<Base, M0, Mixins...>:M0<Foo<Base, Mixins...>>{};

struct Base{
  virtual int A()=0;
  virtual int B()=0;
  virtual ~Base()=default;
};

template<class Base>
struct MixinA:Base{
  int A() final{ return 3; }
};
template<class Base>
struct MixinB:Base{
  int B() override{ return -1; }
};
template<class Base>
struct MixinB2:Base{
  int B() final{ return 0; }
};

using Bob=Foo<Base, MixinA, MixinB2, MixinB>;

std::unique_ptr<Base> pBase=std::make_unique<Bob>();
std::cout << pBase->A() << pBase->B() <<'
';

Leftmost mixins override rightmost ones here.

For the other way around

template<class Base, template<class>class M0, template<class>class...Mixins>
struct Foo<Base, M0, Mixins...>:Foo<M0<Base>, Mixins...>{};

We can remove the Base argument of Foo if you really want. For right-to-left application:

template<template<class>class...Mixins>
struct Foo:Base{};
template<template<class>class M0, template<class>class...Mixins>
struct Foo<M0, Mixins...>:M0<Foo<Mixins...>>{};

it is easy. For left-to-right, you need to get fancier.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...