简体   繁体   English

c++ - 如何在静态成员函数中使用指向成员函数的指针

[英]how to use a pointer to member function inside a static member function in c++

I'm actually trying to use a pointer inside a static function which is in the same class as the functions that i'm trying to use inside the pointer.我实际上是在尝试在静态函数中使用指针,该函数与我试图在指针中使用的函数在同一个类中。 I'm actually asked to use the class like this :我实际上被要求使用这样的类:

class Factory {
    public:
        Factory() = default;
        ~Factory() = default;
        static IOperand* createOperand(eOperandType type, const std::string& value);
    private:
        IOperand* createInt8(const std::string& value);
        IOperand* createInt16(const std::string& value);
        IOperand* createInt32(const std::string& value);
        IOperand* createFloat(const std::string& value);
        IOperand* createDouble(const std::string& value);
        IOperand* createBigDecimal(const std::string& value);
};

This is how I would do if this function wasn't static :如果这个函数不是静态的,我会这样做:

IOperand* Factory::createOperand(eOperandType type, const std::string& value)
{
    IOperand* (Factory::*ptr[6])(const std::string&) = {
            &Factory::createInt8,
            &Factory::createInt16,
            &Factory::createInt32,
            &Factory::createFloat,
            &Factory::createDouble,
            &Factory::createBigDecimal
    };
    return (*this.*ptr[type])(value);
}

ps: eOperandType is just an enum ps:eOperandType 只是一个枚举

You need to know where is the object whose member function should be invoked.您需要知道应该调用其成员函数的对象在哪里。

It doesn't matter how you know it, the point is that you have to know it, in some form or fashion.你如何知道它并不重要,关键是你必须以某种形式或方式知道它。 Perhaps a pointer to the object gets passed as an additional parameter:也许指向对象的指针作为附加参数传递:

IOperand* Factory::createOperand(Factory *obj, eOperandType type, const std::string& value)
{
    IOperand* (Factory::*ptr[6])(const std::string&) = {
            &Factory::createInt8,
            &Factory::createInt16,
            &Factory::createInt32,
            &Factory::createFloat,
            &Factory::createDouble,
            &Factory::createBigDecimal
    };
    return ((*obj).*ptr[type])(value);
}

Or, perhaps, a reference to the object gets passed in, instead of a pointer (resulting in a slight adjustment to the code), or maybe the object is somewhere else entirely.或者,也许,对象的引用被传入,而不是指针(导致对代码的轻微调整),或者对象可能完全在其他地方。 Maybe createOperand() calls some other function that returns a pointer or a reference to the instance of its class, but the point is that a member function cannot be invoked by itself.也许createOperand()调用其他一些函数,该函数返回指向其类实例的指针或引用,但重点是成员函数不能被自身调用。 An object is required whose member function gets invoked.需要一个对象,其成员函数被调用。 That's what a member function is, and how it differs from an ordinary function.这就是成员函数,以及它与普通函数的不同之处。

PS It doesn't matter whether all of this is "inside a static member function", or not. PS 是否所有这些都在“静态成员函数内”并不重要。 This is not a factor.这不是一个因素。 The only factor is that inside a non-static member you always have this as an object that's available to you.唯一的因素是,在非静态成员中,您始终将this作为可供您使用的对象。 But there is no law that requires you to invoke this 's member, via a pointer.但是没有任何法律要求您通过指针调用this的成员。 If you have some other instance of the same class, somewhere, you can invoke its member function, instead of this one's.如果你有同一个类的其他实例,在某处,你可以调用它的成员函数,而不是this

No matter where you use the member function pointer to call a method, you need an object to do so.无论在何处使用成员函数指针调用方法,都需要一个对象来执行。 In your "This is how I would do if this function wasn't static" version you call the method on the current object this .在你的“如果这个函数不是静态的,我会怎么做”版本中,你调用当前对象this上的方法。 In the static method, you need a different object of type Factory , because there is no this in the static method.在静态方法中,您需要一个不同的Factory类型对象,因为静态方法中没有this I suspect that actually all methods of Factory should be static , though not touching that, you can do this:我怀疑实际上Factory所有方法都应该是static ,尽管没有触及,但您可以这样做:

struct IOperand {};
class Factory {
    public:
        Factory() = default;
        ~Factory() = default;
        static IOperand* createOperand(size_t type, const std::string& value) {
            Factory f;
            IOperand* (Factory::*ptr[6])(const std::string&) = {
                &Factory::createInt8,
                &Factory::createInt16,
                &Factory::createInt32,
                &Factory::createFloat,
                &Factory::createDouble,
                &Factory::createBigDecimal
            };
            return (f.*ptr[type])(value);
        }
    private:
        IOperand* createInt8(const std::string& value);
        IOperand* createInt16(const std::string& value);
        IOperand* createInt32(const std::string& value);
        IOperand* createFloat(const std::string& value);
        IOperand* createDouble(const std::string& value);
        IOperand* createBigDecimal(const std::string& value);
};

谢谢大家的回答,我现在对这个主题和我的项目工作有了更好的理解。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM