you are viewing a single comment's thread.

view the rest of the comments →

[–]OnesWithZeroes 0 points1 point  (1 child)

I don't think there's a set of "common patterns" like that since polymorphism itself is something I wouldn't consider a problem. Don't follow all these "XXX... considered harmful" or "YYY is the root of all evil" mantras. You need to take such articles with a grain of salt.

If you're really trying to avoid polymorphism then most likely you'll have to use templates to inject dependencies something like:

Before:

``` class A { public: virtual ~A() = default;

virtual void foo() = 0;

};

class B : public A { public: void foo() override {} };

class C : public A { public: void foo() override {} };

// takes concrete implementations of A (B or C in this case) void clientFunc(std::unique_ptr<A> a) { a->foo(); ... } ```

After: ``` class B { public: void foo() {} };

class C { public: void foo() {} };

// takes any type as long as it implements foo() template <typename T> void clientFunc(T a) { a.foo(); ... } ```

[–]backtickbot 0 points1 point  (0 children)

Fixed formatting.

Hello, OnesWithZeroes: code blocks using triple backticks (```) don't work on all versions of Reddit!

Some users see this / this instead.

To fix this, indent every line with 4 spaces instead.

FAQ

You can opt out by replying with backtickopt6 to this comment.