3

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.

5
  • 2
    int *const is a little nonsensical as a return value. If you want a pointer that is not allowed to modify its int, go with int const *. Commented Aug 13, 2010 at 1:43
  • Hello Potatoswatter, int const* or const int* is a pointer to a constant integer. int *const is a constant pointer to an integer. Let me know If I'm wrong, because I barely know C++ =( -Armando. Commented Aug 14, 2010 at 20:34
  • C++ returns by value, so it's meaningless for you to declare a function returning a const value. You get an rvalue back from the function call; there is no way to modify it anyway. Commented Aug 14, 2010 at 21:22
  • @Carl, Yes if you are returning the copy of an object or a PDT, but what happens when you are returning a handle or the address of a member variable and you want the client to point to that address all the time, then in that case you would like to return a constant pointer to the address (or the offset of the member variable obtained from the class definition with respect of the this pointer if you are using a pointers to public members variables) Please let me know if I'm wrong because I would like to fix my incorrect assumptions asp. Thanks, Armando. Commented Aug 14, 2010 at 21:34
  • 1
    For the client to point to an address, it must have a pointer or reference. That variable may be initialized using the return value and such a pointer can be const, but the const-ness of the return value doesn't affect anything. Usually you use a reference declared with & instead of a *const as the semantics are nearly identical. Commented Aug 14, 2010 at 21:51

5 Answers 5

11

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.

Sign up to request clarification or add additional context in comments.

Comments

5

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])( ... )

Comments

2

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 )

Comments

1

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])(...)

Comments

0

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.

1 Comment

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.