Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ -- Questions about function pointers

Tags:

c++

pointers

I have written the following code:

#include "stdafx.h"
#include <iostream>
using namespace std;

double funcA()
{
    return 100.0;
}

int g(double (*pf)())
{
    cout << (*pf)() << endl;
    return 0;
}

int g2(double pf())
{
    cout << pf() << endl;
    return 0;
}

int _tmain(int argc, _TCHAR* argv[])
{
    g(&funcA);  // case I
    g(funcA);   // case II

    g2(funcA);  // case III
    g2(&funcA); // case IV
    return 0;
}

I have run the above code on VS2008 and each function call returns '100'. Here is the question:

Q1> Is there any problem in the code?

Q2> It seems that C++ doesn't make difference between *pf and pf. Is that correct?

Thank you

like image 958
q0987 Avatar asked Nov 17 '10 16:11

q0987


People also ask

Why function pointers are used in C?

Function pointers in C can be used to create function calls to which they point. This allows programmers to pass them to functions as arguments. Such functions passed as an argument to other functions are also called callback functions.

What is function pointer in C with example?

1) Unlike normal pointers, a function pointer points to code, not data. Typically a function pointer stores the start of executable code. 2) Unlike normal pointers, we do not allocate de-allocate memory using function pointers. 3) A function's name can also be used to get functions' address.

What is the use of function pointer?

A function pointer, also called a subroutine pointer or procedure pointer, is a pointer that points to a function. As opposed to referencing a data value, a function pointer points to executable code within memory.

How do you call a function from a function pointer?

Function Pointer Syntax void (*foo)( int ); In this example, foo is a pointer to a function taking one argument, an integer, and that returns void. It's as if you're declaring a function called "*foo", which takes an int and returns void; now, if *foo is a function, then foo must be a pointer to a function.


1 Answers

C++ does, in fact, make a distinction between the types double() and double(*)(), but the difference is subtle. When you pass a function type as an argument to a function, the function-type automatically "degrades" to a function pointer. (This is similar, I suppose, to how an array type degrades to a pointer type when passed as a function argument.)

However, a function type and a function-pointer type are still different types, according to the C++ type-system. Consider the following case:

void g() { }

template <class F>
struct Foo
{
    Foo(const F& f) : func(f)
    { }

    void operator()() { func(); }

    F func;
};


int main ()
{
    Foo<void()> f(g);
    f();
}

This should fail to compile, since you cannot declare a function type as an automatic variable. (Remember, functions are not first-class objects in C++.) So the declaration F func; is invalid. However, if we change our main function to instead instantiate the template using a function pointer, like so:

int main ()
{
    typedef void(*function_pointer)();
    Foo<function_pointer> f(g);
    f();
}

...now it compiles.

like image 94
Charles Salvia Avatar answered Sep 22 '22 01:09

Charles Salvia