I have a class that stores a function pointer to some binary function
class ExampleClass
{
private:
double(*_binaryFunction)(double, double);
}
How can I return this pointer in a const "getter"? This is either surprisingly hard to search for or nobody else has asked this. If the function weren't const
, the syntax would be
double (*GetBinaryFunction())(double, double);
Where do I put the const
qualifier? Like this?
double (*GetBinaryFunction() const)(double, double);
If I wanted to use a typedef, the syntax would be: (right?)
typedef double (*BinaryFunction)(double, double);
BinaryFunction GetBinaryFunction() const;
Not answering this question but still useful: C syntax for functions returning function pointers (does not deal with const member functions)
Also I want to re-iterate that I don't want to return or am dealing with any pointer-to-member.
To address duplicates:
The more precise formulation of this question would be "Where to syntactically put the cv-qualifier in the declaration of a member function returning a function pointer?" The answer turned out to be surprising (in the middle of the declaration, not at the end as is usually the case) and none of the linked questions deal with the question of the const
keyword position, only with broader syntax of function returning function pointer.
How can I return this pointer in a const "getter"?
Examples:
struct ExampleClass
{
double (*_binaryFunction)(double, double);
double (*GetBinaryFunction() const)(double, double) {
return _binaryFunction;
}
typedef double (*BinaryFunctionTypedef)(double, double);
BinaryFunctionTypedef GetBinaryFunction2() const {
return _binaryFunction;
}
using binaryFunctionUsing = double (*)(double, double);
binaryFunctionUsing GetBinaryFunction3() const {
return _binaryFunction;
}
};