Skip to main content
GameDev.net gamedev.net
🔒 Locked

Function pointers

Started by Xtreme Jan 31, 2005 at 10:30 PM 4 replies 650+ views
Original Post
Xtreme
Xtreme
I am a bit confused when it comes to using function pointer's syntax. The code below uses 2 different ways of calling the Print() method. How can the 2nd method be syntacticly correct?

#include <iostream>

using namespace std;

int main()
{
	void (*ptr)(); //pointer to a function
	void Print();
	ptr = Print;

	(*ptr)(); //calls Print()
	ptr();	//calls Print() as well!

	return 0;
}

void Print()
{
	cout << "Hello World\n";
}



Yes! There are kangaroos in Australia but I haven't seen them...yet
Alpha_ProgDes
Alpha_ProgDes
http://publications.gbdirect.co.uk/c_book/chapter5/function_pointers.html
According to that link, the second form has been blessed by the Standard. Therefore you can use it to your heart's content [smile]


edit: took netscape html reference outta link.
Beginner in Game Development?  Read here. And read here.  
Bincho
Bincho
I'm not quite sure why, but according to the _The C++ Programming Language_ that is correct. The only note about it in the book states that dereferencing a pointer to a funtion is optional, as well as using & to get the address. Maybe there are situations where you have to dereference it, such as when returning data to a variable?
Xtreme
Xtreme
well even if it is "blessed by the Standard", it just doesnt make sense!
Yes! There are kangaroos in Australia but I haven't seen them...yet
Erzengeldeslichtes
Erzengeldeslichtes
In all of my usage of Function Pointers (and I've started using them quite a lot), I have NEVER found a need to dereferance a function pointer, nor the need to "get pointer" with &. The only time I have ever needed either is when dealing with pointers to non-static member functions, never pointers to global function. Member function pointers require both, so far as I can tell. At the very least you need to do Object.*FuncPtr, ObjectPtr->*FuncPtr, or this->*FuncPtr (Sorry, no *FuncPtr within a member function... not in VC2k5 at least), and I think you also need &MyObject::Function.

Here's how I think of it:

void SomeFunc();

SomeFunc is equivelent to a pointer to a function (that's not yet defined, note). Therefore, since I can do SomeFunc(), I should be able to do FuncPtr(). Also, since I can assign a pointer to a pointer, I should be able to assign FuncPtr = SomeFunc. After all, a non-inline function is purely a section of executable memory, and when you call a function you're actually being pointed over to the function memory. That to me says that a function call is a call through a (constant) function pointer.

Then by a different school of thought, SomeFunc is more of an object, and so you need to get the reference, and then dereferance the pointer when using it. I suspect that during the comitee meetings they couldn't get everyone to agree and so just said "Let's do both!". That's the only thing that makes sense to me (I think the former makes the most sense).
----Erzengel des Lichtes光の大天使Archangel of LightEverything has a use. You must know that use, and when to properly use the effects.♀≈♂?
Doc
Doc
Quote:
Original post by Erzengeldeslichtes
and I think you also need &MyObject::Function.


That's been my experience, so I think you're right.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.