C++ C++;通过指向基类派生类中成员函数的指针调用

C++ C++;通过指向基类派生类中成员函数的指针调用,c++,design-patterns,member-function-pointers,C++,Design Patterns,Member Function Pointers,我想提出以下设计模式进行讨论。它在基类中实现了一个通用的“getMany”方法,该方法使用派生类中给定的get方法来获取许多实体(这里是为了简化显式类型int)。这意味着任何派生类都必须通知“getMany”方法使用哪个get方法。这是从基类调用派生类中的成员函数指针的情况 我想在讨论中提出的问题是:你能想到什么替代方案,更容易实现同样的目标 谢谢大家! 注:如上所述,在实际案例中,我们当然会将固定类型“int”抽象为模板类型T。 P.P.S.:在基类中将get方法预定义为虚拟方法似乎不是一个好

我想提出以下设计模式进行讨论。它在基类中实现了一个通用的“getMany”方法,该方法使用派生类中给定的get方法来获取许多实体(这里是为了简化显式类型int)。这意味着任何派生类都必须通知“getMany”方法使用哪个get方法。这是从基类调用派生类中的成员函数指针的情况

我想在讨论中提出的问题是:你能想到什么替代方案,更容易实现同样的目标

谢谢大家!

注:如上所述,在实际案例中,我们当然会将固定类型“int”抽象为模板类型T。 P.P.S.:在基类中将get方法预定义为虚拟方法似乎不是一个好的选择,因为它会限制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;
}
#包括
#包括
#包括
#包括
使用名称空间std;
//通过指向基类派生类中重载方法的指针进行调用的示例
类食品库
{
公众:
模板
向量getMany(int(PCLASS::*getEnt)(int)常量,intn,constpclass*PCLASS)常量
{
std::载体e;
int i=0;
e、 调整大小(n);
对于(std::vector::iterator it=e.begin();it!=e.end();++it){
*it=(pClass->*getEnt)(i++);
}
返回e;
};
};
Foo类:公共FooBase
{
公众:
int Moo(int a)const
{
返回a;
};
int Moo(字符a)常量
{
返回(int)a;
};
向量Moos(int n)常数
{
内部(Foo::*f)(内部)常量;
f=&Foo::Moo;
返回getMany(f,n,this);
};
};
int main(int argc,char**args)
{
Foo*myFoo=newfoo();
std::vector res=myFoo->Moos(10);
对于(std::vector::iterator it=res.begin();it!=res.end();++it){

std::cout下面是一个使用函数对象的示例

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++ );
        }
    }
};
auto callMoo = [this] (char i) { return Moo(i); };
getMany(callMoo, n);

您可以在此处使用常规多态性。使用一个抽象方法,通过基类方法中的
this
指针调用该方法。一个简化示例如下:

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();
}
class-FooBase
{
公众:
virtual void abstractCall()=0;//纯虚拟函数,如果需要,可以使其受保护
void baseMethod()
{
this->abstractCall();//必须使用this指针,而不是“.”调用
}
};
类foodderived:publicfoobase
{
公众:
虚拟void抽象调用()
{

C++11中的cout?std::function和lambdas。否则是很好的旧函数对象。谢谢,您能详细说明一下吗?如何将成员函数“Moo”传递给基类方法“getMany”?我会这样做作为回答。非常感谢你的建议!这是我的问题:我使用的基类将由许多不同的派生类继承。因此,在基类中将get方法预定义为虚拟方法似乎不是一个好的选择,因为这将限制get方法的数量和命名。谢谢,这很好!但是:您的代码缺少一个重要的部分“”。那就是“getMany(callMoo,n);”应该是“getMany(callMoo,n);”。请为将来的读者更正这一点。您确定吗?模板参数应该很容易推导。哈,您是对的!它是推导出来的,没有指望gcc来完成这一点:)