views:

180

answers:

5

Maybe is a silly questiion, but I haven't found a solution to what I want. Basically, I would like to declare a pointer to an array of pointers to function pointers to functions with N parameters that returns an const pointer to an int.

The code below is declaring an array of pointers to the function pointers:

  int *const (**fptr[10])(...); // (const int* || int const*) != int *const

As you can see, the only thing is missing ( I think) is the pointer to the code from above.

I'm just a beginner and I'm not using this type of syntax in production, I'm just having fun while learning C++.

Thanks,

Armando.

+3  A: 

You might want to look at The Right-Left Rule. This kind of thing is more closely associated with C; function pointers are less commonly used in C++ because of virtual functions, templates, and such.

Also http://cdecl.org is helpful. I came up with this:

int const * (**(*f)[10])( ... )
Potatoswatter
Thanks the right-left rule is really cool!
Armando Fonseca
+7  A: 

The answer is multiple levels of typedefs to make this close to understandable. If you figure this out tonight, you'll want to still be able to understand it in the morning.

typedef int *const (*myFuncPtr)(...); // pointer to function taking ... and returning constant pointer to int
typedef myFuncPtr myFuncArray[10]; // array of function pointers
myFuncPtrArray *myMonstrosity ; // pointer to array of function pointers

Reading backwards can help you see what we've declared. Starting at the bottom right: We have "myMonstrosity" which a pointer to a myFuncPtrArray which is an array of 10 elements of myFuncPtr which is a function taking "..." pointer which returns a constant pointer to an integer.

SoapBox
+2  A: 

For this kind of "having fun while learning", I recommend cdecl.org which translates C (and some C++) type signatures into plain English and back. In fact, the random type they just gave me was

declare bar as const pointer to array 5 of pointer to function (int) returning const pointer to char

which, as shown immediately on the website, is

char * const (*(* const bar)[5])(int )
Cubbi
A: 

Here's what I think it should be:

int* const (** (*fptr) [10])(...);

One trick I found for figuring this stuff out with g++ is to build up the type with typedefs (like SoapBox did), then pass an instance of the final type to a type-deducing template function, like this:

template <typename T>
void foo(T t) {}

int main(void)
{
  foo(fptr);
}

compile this, then dump the symbol table using nm. Pipe the result through c++filt:

nm a.out | c++filt | grep foo
0000000100000eef T void foo<int* const (** (*) [10])(...)>(int* const (** (*) [10])(...))

The stuff between the angle brackets is the type, at least as far as the compiler is concerned.

SCFrench
I do something similar, but I assign the function pointer to an invalid type, which gives a nice error message about a bad cast. There's also `__PRETTY_FUNCTION__` in gcc to print the function name with parameters in a "readable" form.
Dennis Zickefoose
A: 

It is better to also get into the practice of constructing the declaration rather than deciphering an already existing one. Here's how I proceed:

pointer to an

*p

array of pointers

(*p)[10] // Parenthesis is required as [] has higher precedence

to function pointers (read as "pointer to pointer to function")

XXXX (**(*p)[10])(YYYY)   // Will fill in XXXX and YYYY shortly below

with N parameters that returns an const pointer to an int

int *const (**(*p)[10])(...)

So the final declaration is:

int *const (**(*p)[10])(...)
Chubsdad