简体   繁体   English

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

[英]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.

相关问题 C ++:在另一个派生类的对象的基本指针上调用(派生的)成员函数 - C++: call (derived's) member function on base pointer of a different derived class's object C++ 从基类指针访问派生类成员 - C++ Access derived class member from base class pointer C ++:在不同类的派生对象的基本指针上调用静态成员函数 - C++: call static member function on base pointer of different class's derived object C ++将派生类的成员指针转换为基类指针 - c++ cast member pointer of derived Class to base class pointer 派生类中的C ++成员函数模板,如何从基类中调用它? - C++ member function template in derived class, how to call it from base class? 如何从指向基类类型的指针调用派生类函数(C ++) - How to call derived class function from pointer to base class type (C++) 通过指针的C ++函数调用可以访问隐藏的基类成员函数吗? - c++ function call via pointer can acess hidden base class member function? 推导类型的基类成员的指针 - Deduced type for pointer-to-member of base class 指向派生类对象的C ++基类指针不调用派生相等运算符函数 - C++ Base class pointer pointing to derived class object doesn't call derived equal operator function C++ function 解析基 class 指向派生 class 指针的指针 - C++ function resolving base class pointer to derived class pointer
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM