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

A pointer to method in C++

Started by The C modest god Feb 4, 2004 at 3:53 AM 13 replies 650+ views
Original Post
The C modest god
The C modest god
How do you write a pointer to method in C++?
It's all about the wheel.Never blindly trust technoligy.I love my internal organs.Real men don't shower. Quote: Original post by Toolmaker Quote:
SiCrane
SiCrane
I assume by method you mean class member function.

class A {
public:
void func(void);
};

typedef void (A::*pfn)(void);

pfn a_func = &A::func;
Jingo
Jingo
Or for non-member functions


int func(){return 0;}

int (*pfunc)() = func;



[edited by - Jingo on February 4, 2004 5:03:02 AM]
The C modest god
The C modest god
I have the follwoing code:

class A {
private:
static void InitialScale ();
static void (ImageScaler::*pInitialScale)();
};

void
A::InitialScale ()
{
A:InitialScale();
}


It says on the last line that the expression does not evaluate to a function, why is that?
It's all about the wheel.Never blindly trust technoligy.I love my internal organs.Real men don't shower. Quote: Original post by Toolmaker Quote:
SiCrane
SiCrane
Because a member function pointer isn't a function. In order to use a member function pointer you need to bind it to an object somehow.

void A::InitialScale() {
// create an ImageScaler on the stack, bind it to pInitialScale
// and call
ImageScaler is;
(is.*A::pInitialScale)();

// create an ImageScaler on the heap, bind it to pInitialScale
// and call
ImageScaler * p_is = new ImageScaler;
(p_is->*A::pInitialScale)();
delete p_is;
}


edit: !@$@ Smiley code

[edited by - SiCrane on February 4, 2004 5:23:24 AM]
The C modest god
The C modest god
quote:
Original post by SiCrane
Because a member function pointer isn''t a function. In order to use a member function pointer you need to bind it to an object somehow.

void A::InitialScale() {
// create an ImageScaler on the stack, bind it to pInitialScale
// and call
ImageScaler is;
(is.*A::pInitialScale)();

// create an ImageScaler on the heap, bind it to pInitialScale
// and call
ImageScaler * p_is = new ImageScaler;
(p_is->*A::pInitialScale)();
delete p_is;
}


edit: !@$@ Smiley code

[edited by - SiCrane on February 4, 2004 5:23:24 AM]

This code doesnt work
It's all about the wheel.Never blindly trust technoligy.I love my internal organs.Real men don't shower. Quote: Original post by Toolmaker Quote:
SiCrane
SiCrane
Compiles fine under MSVC .NET 2003.
The C modest god
The C modest god
I have the code:

void
ImageScaler::InitialScale ()
{
ImageScaler is;

(is.*ImageScaler:InitialScale)();
}

and get the following link errors:

error LNK2001: unresolved external symbol "private: static void (__thiscall ImageScaler::* ImageScaler:InitialScale)(void)" (?pInitialScale@ImageScaler@@0P81@AEXXZQ1@)
Debug/V0P01.exe : fatal error LNK1120: 1 unresolved externals
It's all about the wheel.Never blindly trust technoligy.I love my internal organs.Real men don't shower. Quote: Original post by Toolmaker Quote:
Qw3r7yU10p!
Qw3r7yU10p!
any static member of a class needs to also be defined in a seperate cpp file

void (ImageScaler::*pInitialScale)() = 0;

haven't checked the syntax

edit: using typedefs when you're dealing with pointers to functions can make things a lot easier to read.

eg

int functionTakingDoubleAndString(double, string);

typedef int(*FunctionPtr)(double, string);

FunctionPtr p = functionTakingDoubleAndString;

int result = p(10.5, "hello");

[edited by - petewood on February 4, 2004 7:03:08 AM]
The C modest god
The C modest god
I have changed it to the foolwing code:

class ImageScaler {
private:
static void InitialScale ();
static void (*pInitialScale)();
};

void
ImageScaler::InitialScale ()
{
(ImageScaler:InitialScale)();
}

but I get the following errors:
error LNK2001: unresolved external symbol "private: static void (__cdecl* ImageScaler:InitialScale)(void)" (?pInitialScale@ImageScaler@@0P6AXXZA)
Debug/V0P01.exe : fatal error LNK1120: 1 unresolved externals

Why is that?

It's all about the wheel.Never blindly trust technoligy.I love my internal organs.Real men don't shower. Quote: Original post by Toolmaker Quote:
Cygon
Cygon
Here's an example which show initialization, assignment and call of a function pointer to a class' method.

#include <iostream>
#include <string>
using namespace std;

class MyClass {
public:
// Always initialize function pointer to be safe

MyClass() : m_fnShowMessage(showHelloMessage) {}

// Assign function pointer

void foo() { m_fnShowMessage = showByeMessage; }

// Call method pointed to by function pointer

void showMessage(const string &s) { (this->*m_fnShowMessage)(s); }

private:
// Function pointer

void (MyClass::*m_fnShowMessage)(const string &);

// Method 1 for pointer

void showHelloMessage(const string &s) { cout << "Hello " << s << endl; }
// Method 2 for pointer

void showByeMessage(const string &s) { cout << "Goodbye " << s << endl; }
};

int main() {
MyClass m;

m.showMessage("world");
m.foo();
m.showMessage("world");
}


-Markus-


[edited by - Cygon on February 4, 2004 7:10:16 AM]
Professional C++ and .NET developer trying to break into indie game development.
Follow my progress: http://blog.nuclex-games.com/ or Twitter - Topics: Ogre3D, Blender, game architecture tips & code snippets.
The C modest god
The C modest god
quote:
Original post by The C modest god
I have changed it to the foolwing code:

class ImageScaler {
private:
static void InitialScale ();
static void (*pInitialScale)();
};

void
ImageScaler::InitialScale ()
{
(ImageScaler:InitialScale)();
}

but I get the following errors:
error LNK2001: unresolved external symbol "private: static void (__cdecl* ImageScaler:InitialScale)(void)" (?pInitialScale@ImageScaler@@0P6AXXZA)
Debug/V0P01.exe : fatal error LNK1120: 1 unresolved externals

Why is that?




Why does it says unresolved external to the static function pointer member, as if it looks for the body of the function in the code?
It's all about the wheel.Never blindly trust technoligy.I love my internal organs.Real men don't shower. Quote: Original post by Toolmaker Quote:
mbenic
mbenic
As mentioned earlier in the thread, a function pointer member is still a member variable. So a static function pointer needs to be 'defined' (I'm not sure of the correct terminology). So for example, if you have the following class in Foo.h:

typedef void (*tFuncPointer)(int param1);

class Foo
{
public:
static int miIntVal;
static tFuncPointer mpfFuncVal;

.....

};

Both the static integer member and the static function member must be declared in a source file. Eg. in Foo.c:

int Foo::miIntValue;
tFuncPointer Foo::miFuncPointer;

Note two things here, you may initialize the members at this point (though you dont have to) and the above will work for your inline definition of the function pointer type, but for readability sake (especially if you will be passing function pointers as parameters to other functions) I highly recommend you use typedefs for these kinds of things.

I think perhaps the problems you are having have more to do with static members and less with function pointers. Try reading up on static members..


[edited by - mbenic on February 4, 2004 8:07:40 AM]
Qw3r7yU10p!
Qw3r7yU10p!
try it with any class and any static member. load this into your compiler and uncomment the commmented out line to get rid of the error code.


struct A {
static int count;
};

//int A::count;//need to define the static instance (there is only one)


int main() {
A a;
return 0;
}

Topic Locked

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

Sign in to reply to this topic.