Syntax for declaring const member function returning a bare function pointer, without typedefs?

Viewed 74

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.

3 Answers

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;
    }
};

To add upon what has been said, I think it's also important to mention the correct syntax for references.


  • for the normal reference:
double (*&GetBinaryFunction())(double, double)

  • and for the const& version:
double (*const& GetBinaryFunction() const)(double, double)

the first const describes the return type (a function pointer that is a const reference) and the second const describes the ExampleClass member constness.


example:

#include <iostream>
#include <cmath> //for std::pow

class ExampleClass {
public:
    double (*&GetBinaryFunction())(double, double) {
        return this->_binaryFunction;
    }
    double (*const& GetBinaryFunction() const)(double, double) {
        return this->_binaryFunction;
    }

private:
    double(*_binaryFunction)(double, double);
};

void foo(ExampleClass& example, double a, double b) {

    example.GetBinaryFunction() = std::pow;

    std::cout << "foo( & ) : " << example.GetBinaryFunction()(a, b) << '\n';
}

void bar(const ExampleClass& example, double a, double b) {
    std::cout << "bar( const& ) : " << example.GetBinaryFunction()(a, b) << '\n';
}

int main() {
    ExampleClass example;
    foo(example, 2, 5);
    bar(example, 2, 6);
}

output:

foo( & ) : 32
bar( const& ) : 64

if you don't like the long syntax and don't want to use typedef or using you can also simply use auto:

auto& GetBinaryFunction() {
    return this->_binaryFunction;
}
const auto& GetBinaryFunction() const {
    return this->_binaryFunction;
}

it will compile into the same code as above. to not use reference, simple remove the & from the auto.

You can use an auto return type

class ExampleClass
{
    auto get() const { return _binaryFunction; }
private:
    double(*_binaryFunction)(double, double);
};
Related