[英]C++ call via pointer-to-member function in a derived class from the base class
I would like to pose the following design pattern to discussion. 我想提出以下设计模式进行讨论。 It implements a universal "getMany" method in the base class that uses a given get-method from the derived class to get many entities (here for simplification of explicit type int).
它在基类中实现了一个通用的“getMany”方法,该方法使用派生类中的给定get方法来获取许多实体(这里是为了简化显式类型int)。 This means that any derived class has to inform the "getMany" method which get-method to use.
这意味着任何派生类都必须通知“getMany”方法使用哪个get-method。 This is a case of calling a pointer-to-member function in a derived class from the base class.
这是从基类调用派生类中的指向成员函数的情况。
What I would like to pose to discussion: what alternative, easier patterns to achieve the same can you think of? 我想提出什么讨论:你能想到什么替代方案,更容易实现同样的模式?
Thank you! 谢谢!
PS: As noted above in a real case one would of course abstract the fixed type "int" to a template type TPPS: predefining the get-methods as virtual methods in the base class did not seem a good option, since it would restrict the number of and naming of the get-methods. PS:如上所述,在实际案例中,当然会将固定类型“int”抽象为模板类型TPPS:预定义get-methods,因为基类中的虚方法似乎不是一个好的选择,因为它会限制get方法的数量和命名。
#include <iostream>
#include <memory>
#include <algorithm>
#include <vector>
using namespace std;
// EXAMPLE FOR CALL VIA POINTER TO OVERLOADED METHOD IN DERIVED CLASS FROM BASE CLASS
class FooBase
{
public:
template<class PCLASS>
std::vector<int> getMany(int (PCLASS::*getEnt)(int) const, int n, const PCLASS *pClass) const
{
std::vector<int> e;
int i = 0;
e.resize(n);
for (std::vector<int>::iterator it = e.begin(); it!=e.end(); ++it) {
*it = (pClass->*getEnt)( i++ );
}
return e;
};
};
class Foo : public FooBase
{
public:
int Moo(int a) const
{
return a;
};
int Moo(char a) const
{
return (int)a;
};
std::vector<int> Moos(int n) const
{
int (Foo::*f)(int)const;
f = &Foo::Moo;
return getMany<Foo>(f, n, this);
};
};
int main(int argc, char **args)
{
Foo* myFoo = new Foo();
std::vector<int> res = myFoo->Moos(10);
for (std::vector<int>::iterator it = res.begin(); it!=res.end(); ++it) {
std::cout << *it;
}
return 1;
}
Here is an example using function objects. 以下是使用函数对象的示例。
class FooBase
{
public:
template< typename FunctorType >
std::vector<int> getMany(FunctorType const & functor, int n)
{
std::vector<int> e;
int i = 0;
e.resize(n);
for (std::vector<int>::iterator it = e.begin(); it!=e.end(); ++it) {
*it = functor( i++ );
}
}
};
With this, client code can call getMany using lambdas (c++11) or create their own function objects. 有了这个,客户端代码可以使用lambdas(c ++ 11)调用getMany或创建自己的函数对象。
auto callMoo = [this] (char i) { return Moo(i); };
getMany(callMoo, n);
You can just use regular polymorphism here. 你可以在这里使用常规多态。 Have an abstract method that you call via the
this
pointer from the base class method. 有一个抽象方法,您可以通过基类方法中的
this
指针调用。 A simplified example is: 一个简单的例子是:
class FooBase
{
public:
virtual void abstractCall() = 0; // pure virtual function, could make protected if you wanted
void baseMethod()
{
this->abstractCall(); // MUST use the this pointer, not a "." call
}
};
class FooDerived : public FooBase
{
public:
virtual void abstractCall()
{
cout << "my concrete call!" << endl;
}
};
void bar()
{
FooDerived test;
test.baseMethod();
}
I hope that gives you the basic idea. 我希望能给你一个基本的想法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.