Inherit a class using a template function

I would like to create a base class that will be inherited by other objects so that they can be stored in one container. This base class will contain a template method that defines the function as the setter or receiver used to access the buffer in a multi-threaded system. I want to do something like this guy, but not quite sure how to implement Linky. I would also like to be able to use a virtual function in the database and define functionality in derived classes, I know that you actually do not have a virtual template function, but there is a way to encode it in such a way that it acts as a concept of a virtual template function. Below is a rough example of how I would like the layout to be. The do_work method called through the callback. The callback is passed to the stream as an argument.

class A {
    template<typename R, typename P>
    virtual R do_work(P param) = 0;
}

class B : public A {
    template<void,int> // declare as setter
    R do_work(P param){/*do something*/ return R;}

}

class C : public A {
    template<int,void> // declare as getter
    R do_work(P param){/*do something*/ return R;}

}
+3
source share
3 answers

, , . , , . . do_work, ( ). , , , , .

0

, , , A do_work A: .

R B C, .

do_work , , , A, - A, B C, .

+1

, , , . - , .

, .

, , perf, , , .

0

Source: https://habr.com/ru/post/1770320/


All Articles