Returning function pointer type

∥☆過路亽.° 提交于 2019-11-27 10:55:27
int (*getFunc())(int, int) { … }

That provides the declaration you requested. Additionally, as ola1olsson notes, it would be good to insert void:

int (*getFunc(void))(int, int) { … }

This says that getFunc may not take any parameters, which can help avoid errors such as somebody inadvertently writing getFunc(x, y) instead of getFunc()(x, y).

yosim

You can probably do something like:

int foo (char i) {return i*2;}

int (*return_foo()) (char c)
{
   return foo;
}

but god, I hope I'll never have to debug you code....

ill leave this here since it was a bit trickier than answers already given, as it takes a function pointer

(int (__cdecl *)(const char *))

and returns a function pointer

(int (__cdecl *)(const char *))

#include <stdio.h>

int (*idputs(int (*puts)(const char *)))(const char *) {
    return puts;
}

int main(int argc, char **argv)
{
    idputs(puts)("Hey!");

    return 0;
}

This is a stupid example, but it's simple and it does not give errors. It's just about declaring static functions:

#include <stdio.h>
#include <stdlib.h>

void * asdf(int);
static int * hjkl(char,float);

main() {
  int a = 0;
  asdf(a);
 }


void * asdf(int a) {return (void *)hjkl; }
static int * hjkl(char a, float b) {int * c; return c;}

While wrapping some C code in C++ classes, I had the same desire as the original poster: return a function pointer from a function without resorting to typedef'ing the function pointer prototype. I hit a problem with C++ const correctness which I thought was worth sharing, even if it's a little off-topic (C++) but it does relate directly to the original question: the syntax for returning a C function pointer without resorting to a typedef.

The code below defines a class A which stores a function pointer and exposes it to the outside world through the get_f() call. This is the function that should return a function pointer without a typedef.

The point (which stumped me for some time) was how to declare that get_f() was a const function, i.e. it wouldn't alter A.

The code contains 2 variants: the first uses a typedef for the function pointer prototype, whilst the second writes everything out in full. The #if switches between the two.

#include <iostream>

int my_f(int i)
{
  return i + 1;
}

#if 0 // The version using a typedef'ed function pointer

typedef int (*func_t)(int);

class A
{
public:
  A(func_t f) : m_f(f) {}
  func_t get_f() const { return m_f; }

private:
  func_t m_f;
};

int main(int argc, char *argv[])
{
  const A a(my_f);
  std::cout << "result = " << a.get_f()(2) << std::endl;
}

#else // The version using explicitly prototyped function pointer    

class A
{
public:
  A(int (*f)(int)) : m_f(f) {}
  int (*get_f() const)(int) { return m_f; }

private:
  int (*m_f)(int);
};

int main(int argc, char *argv[])
{
  const A a(my_f);
  std::cout << "result = " << a.get_f()(2) << std::endl;
}

#endif

The expected/desired output is:

result = 3

The key point is the position of the const qualifier in the line:

int (*get_f() const)(int) { return m_f; }

I think you've got three options:

  1. Stick with typedef. At the end of the day, it's typedef's job.
  2. Return void* and the casting it.
  3. Reconsider your software architecture. Perhaps you could share with us what you're trying to achieve and see if we can point you toward a better direction.

You can write the following code(It only works in C++11 and above):

//C++11
auto func(...) {
    int (*fptr)(...) ret = ...
    //Do sth.
    return ret;//C++11 compiler will automatically deduce the return type for you
}

Or, if you do not like automatic return type deduction, you can specified the type at the end of the function(Same as above, only in C++11 and above):

//C++11
auto func(...) -> int (*)(...) { /* Do sth. */ }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!