Jump to content
  • Advertisement
Sign in to follow this  
MagTDK

Function Pointer Problem

This topic is 4873 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have a test class with a function pointer that I want to dynamically change to point to another implementation. I don't use function pointers that often so I'm a little confused on the syntax. Right now I'm calling a regular function to call the function pointer but that's not what I want. I want to be able to call the function directly and have it look like a normal function. If you look at my main function and class source code you'll see what I'm doing.
// MyClass.h ////////////////////////////////////////////

#pragma once

class MyClass
{

public: 

	static bool m_IsFirst;

	int (MyClass::*FunctionPointer)(int a);
	int FirstFunction(int a) { return a*2; }
	int SecondFunction(int a) { return a*4; }

	int CallFunctionPointer(int a) { return (this->*FunctionPointer)(a); }

	MyClass(void);
	~MyClass(void){};
};

// MyClass.cpp //////////////////////////////////////////

#include "MyClass.h"

bool MyClass::m_IsFirst = false;

MyClass::MyClass(void)
{
	if(m_IsFirst) {
		FunctionPointer=MyClass::FirstFunction;
	} else {
		FunctionPointer=MyClass::SecondFunction;
	}
}

And here is my main:
#include "stdafx.h"

#include "MyClass.h"
#pragma comment(lib, "Test.lib")

int _tmain(int argc, _TCHAR* argv[])
{
	MyClass::m_IsFirst = true;
	MyClass* pTest = new MyClass();

	// LOOK HERE: This works but I have to call another function to call my function pointer.
	int a = pTest->CallFunctionPointer(5);

        // I want something like this:
        // int a = pTest->FunctionPointer(5); // See how it's called directly

	return 0;
}

Share this post


Link to post
Share on other sites
Advertisement
(pTest->*(pTest->FunctionPointer))(5);
Since you are not inside the class you need to fetch the function pointer from the object. This makes sense if you think that you might wanna use a function pointer from one object on another object.
(obj1->*(obj2->ptr))(...);

Share this post


Link to post
Share on other sites
hmm...originally I was using my static bool value to pick the path every function call like so:


int MyClass::foo(int a)
{
if (m_IsFirst) {
// Run time critical code
} else {
// Run time critical code
}
}



I thought I could speed it up by using function pointers but after looking at the disassembly between the 3 methods I come up with:

8 assembly instructions for this: int a = (pTest->*(pTest->FunctionPointer))(5);
4 assembly instructions for this: int a = pTest->CallFunctionPointer(5);
4 assembly instructions for this: int a = pTest->foo(5); // above function

No speed increase as far as I'm reading it. I guess I'll go back to using my bool test.

Share this post


Link to post
Share on other sites
When you use member-function pointers, you MUST put an ampersand before the function name. ie,

MyClass::MyClass(void)
{
if(m_IsFirst) {
FunctionPointer=&MyClass::FirstFunction;
} else {
FunctionPointer=&MyClass::SecondFunction;
}
}



If it's "option 1" or "option 2", then there's going to be no speed increase (in fact, you're going to have worse speed).

When it's "option 1", "option 2", ..., "option 245", "option 246", etc, well, then you see a speed increase. What you need to determine is which is faster:

std::string Test = "16";
if(Test == "1")
{
/*Do Stuff*/
}
else if(Test == "2")
{
/*Do Stuff*/
}
//...
else if(Test == "16")
{
/*Do Stuff*/
}

or

std::string Test = "16";
this->*m_funtable[GetIndex(Test)]();


In other words, maximum comparisons vs. finding index, table lookup, and pointer jump. In this case, both comparisons and index locating are (very) non-trivial.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!