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
((*this).*ptr[type])(value)or(*this->*ptr[type])(value).Factoryshould be static?IOperand*which implies theynewand object and return a pointer to it. This is an outdated strategy that should not be used anymore. Read aboutstd::unique_ptrandstd::make_unique. Your return type should bestd::unique_ptr<IOperand>.