Original Post
How do you write a pointer to method in C++?
class A {
public:
void func(void);
};
typedef void (A::*pfn)(void);
pfn a_func = &A::func;
int func(){return 0;}
int (*pfunc)() = func;
class A {
private:
static void InitialScale ();
static void (ImageScaler::*pInitialScale)();
};
void
A::InitialScale ()
{
A:
InitialScale();
}
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;
}
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]
void
ImageScaler::InitialScale ()
{
ImageScaler is;
(is.*ImageScaler:
InitialScale)();
}
class ImageScaler {
private:
static void InitialScale ();
static void (*pInitialScale)();
};
void
ImageScaler::InitialScale ()
{
(ImageScaler:
InitialScale)();
}
#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");
}
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?
struct A {
static int count;
};
//int A::count;//need to define the static instance (there is only one)
int main() {
A a;
return 0;
}
This topic has been locked by a moderator. New replies are not allowed.
With your permission, GameDev.net uses analytics cookies to understand how people use the platform. You can accept analytics or continue with necessary cookies only. Learn more