Jump to content
Sign in to follow this  
linuxuser

C/C++ Programming: Pointers to functions

Recommended Posts

Pointers to functions

Every C/C++ programmer knows what pointer ( * ) is. Pointer is a integral part of C/C++ programming. I think pointer adds spice to C/C++. Without pointer C/C++ programming will be dull as programing in Visual Basic, or Java, or ASP/HTML/ColdFusion/PHP ( Are they even programming languagues/ oh they r scripts, how dull !!! ).

 

Anyways, Lets talk about Pointers to functions.

Data isn't the only thing you can have a pointer to. You can also have a pointer to a function. Function pointers allow you to use functions during run time that you either didn't have available during development or that you aren't sure of the order in which the functions should be called.

 

Typedef

Function pointers are a bit more complicated in that they aren't a predefined type. They don't have a predefined size nor do they have a specific set of inputs or outputs. So before we can cast a pointer as a function type, we have to declare the function inputs and outputs. We do this using typedef:

 

typedef int (*Function_Pointer_Type)(int);

 

Notice the asterisk in front of the variable type name Function_Pointer. This means that whatever we cast with this new type, it is already a pointer. The inputs and outputs of this function are 'int's. These inputs and outputs are sometimes referred to as a function's "signature". Here is an example of a function that has the same signature as the above function type:

 

int Square(int num)

{

return num * num;

}

 

Assign

The name of the function itself gives us a pointer to it. All we have to do is leave off the parenthesis ()'s like so:

 

Funtion_Pointer_Type function_pointer; //Already a pointer, remember?

function_pointer = Square; //Leave off the ()'s and it's a pointer to the function

 

int output = function_pointer(4); //use the pointer like the function!

 

Class Member Functions

You can also get pointers to class member functions this way too. However, in order to get a pointer to those functions, they must be declared static in the class definition. Unfortunately, declaring class member functions static prevents accessing the other class members.

 

class foo

{

public:

foo(); //Construct is private so no one outside the class can call it

static int Square(int num); //Static class member functions can pass pointers

int Add(int num); //Non static member functions will not give out it's pointer

}

 

foo::foo()

{

}

 

int foo::Square(int num)

{

return num * num;

}

 

int foo::Add(int num)

{

return num + 5;

}

 

foo* fooclass; //declare a class of type foo

Funtion_Pointer_Type function_pointerA, function_pointerB;

function_pointerA = fooclass.Square; //no problem

function_pointerB = fooclass.Add; //will not compile, function must be static

 

 

Share this post


Link to post
Share on other sites

C++ does allow you to create a pointer to a non-static class member.

See the code below

 

class foo

{

int num;

public:

foo(int n);

int Square();

};

 

foo::foo(int n)

: num(n)

{

}

 

int foo::Square()

{

return num*num;

}

 

main()

{

foo o(10);

foo *po = &o;

 

// declare a pointer to class member function

int (foo::*func)();

// initialize the function pointer

func = &foo::Square;

 

// call through an object

cout

// call through a pointer to an object

cout *func)()

}

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.