Result(*pfunc)(Arg); ???

1
2
3
4
5
6
7
8
9
10
template <class Arg, class Result>
  class pointer_to_unary_function : public unary_function <Arg,Result>
{
protected:
  Result(*pfunc)(Arg);
public:
  explicit pointer_to_unary_function ( Result (*f)(Arg) ) : pfunc (f) {}
  Result operator() (Arg x) const
    { return pfunc(x); }
};


Some points here just couldn't figure out easily...

As you can see this is the definition in the cplusplus.com reference about this class and I have to ask :

1. In the protected part : A function (as I use frequently) is technically a pointer,is it ? so when we use deferencing * it returns a class or what ?

2. What does the keyword explicit means.

3. explicit pointer_to_unary_function ( Result (*f)(Arg) )

What kind of signature is that,I think f should be a class calling its constructor but I should be wrong cuz it makes things too complex.

4. Result(*pfunc)(Arg); also this kind of variable declaration is hard-to-understand for me :(
Line 2 declares a member variable which is a pointer to a function which takes a single argument of type Arg and returns a value of type Result. The syntax is a bit weird, but I suppose it's consistent with other declarations.

explicit is a keyword for constructors. Consider this class:
1
2
3
4
5
6
struct Foo{
   ...
   Foo(int x) {
      ...
   }
};

Now in your main you could write Foo f = 5;. This is an implicit call to the constructor. If you declare the c-tor explicit, you must use Foo f(5); syntax.
2 questions left. ?
4. Result(*pfunc)(Arg); also this kind of variable declaration is hard-to-understand for me :(


Start in the middle.

pfunc is a.... (look left)
pointer to a function that... (look right)
takes a single parameter, of type Arg, and... (look left)
returns an object of type Result.
So *pfunc is a function takes arg as argument and returns result ?

So pfunc is a pointer ?

and main(),for example,is a pointer to,so I could make *main ??
If pfunc is a function pointer, the syntax to call it is (*pfunc)(arguments).
main() is a function. You could write void (*ptr)() = main; but that doesn't make it a pointer, the same way that array isn't a pointer.
void (*ptr)() = main; That will not compile, because the types are different (main must return int)

1
2
3
4
5
6
7
8
9
10
11
template <class Arg, class Result>
  class pointer_to_unary_function : public unary_function <Arg,Result>
{
  typedef Result (*function_pointer)(Arg);
protected:
  function_pointer pfunc;
public:
  explicit pointer_to_unary_function ( function_pointer f ) : pfunc (f) {}
  Result operator() (Arg x) const
    { return pfunc(x); }
};
¿better?
C++ implicitly considers every use of a function that it's not call to be address assigning (some pointer is taking the function address). Its similar to the way the name of an array gives a pointer to the first element of the array.

So if type matches you can do:

1
2
int (*fp)(); //meaning fp is a pointer to a function returning int and having void argument
fp = main;


The second one I think it's legal (syntactically I mean) but it's not recommended. The "correct" form would have been:
fp = &main; which has exactly the same result.

As far as I know main() should not be called or be called, so you can do it with other functions also.
Last edited on
int (*fp)()

and

int (*fp)(int)

have a different pointers addresses ?


And also how about


1
2
3
4
5
func(int)

func(void)

func(char)
Last edited on
And important thing : Function (like main) is an object pointer ? and *func deferences it to object ?

That's sound easy now.
int(*fp)() and int(*fp)(int) and etc. all point to different types of functions.
Why do you keep insisting that a function is a pointer. Can you apply () to pointers? In reality a function is a section of code stored in memory, so you could call it an object and it is perfectly natural to be able to have a pointer to it, but you never get to manipulate it like an object. If you want your functions to be objects, learn Haskell.
No I'm just asking...not messing things up.Thread closed.
Topic archived. No new replies allowed.