Static Pointer to Functions inside Class?

B

Bryan Parkoff

I am able to create pointer to function variable as 's2'. m_func1()
function's memory address is copied into s2 variable. Then s2 acts like
pointer to function and is executed without any problem. I do not want
function's memory address to be copied into s2 at run-time. How can I
define at static time? Here is a code below. Notice comment after static
const pmfn1 s2.

class Testpm

{

public:

void m_func1();

int m_num;

typedef void (Testpm::*pmfn1)();

void func();

// static const pmfn1 s2 = &Testpm::m_func1; // error

pmfn1 s2; // OK

static const int n = 2;

};

void Testpm::m_func1()

{

int a = 5;

}

void Testpm::func()

{

s2 = &Testpm::m_func1;

(this->*s2)();

}

int main()

{

Testpm pTestpm;

pTestpm.m_num = 10;

pTestpm.func();

}
 
I

Ian Collins

Bryan said:
I am able to create pointer to function variable as 's2'. m_func1()
function's memory address is copied into s2 variable. Then s2 acts like
pointer to function and is executed without any problem. I do not want
function's memory address to be copied into s2 at run-time. How can I
define at static time? Here is a code below. Notice comment after static
const pmfn1 s2.

class Testpm

{

public:

void m_func1();

int m_num;

typedef void (Testpm::*pmfn1)();

void func();

static const pmfn1 s2 = &Testpm::m_func1; // error
static const pmfn1 s2;
pmfn1 s2; // OK

static const int n = 2;

};

In one and only one translation unit:

const Testpm::pmfn1 Testpm::s2 = &Testpm::m_func1;


Only static const integral types can be initialised where they are
declared in a class.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,143
Messages
2,570,822
Members
47,368
Latest member
michaelsmithh

Latest Threads

Top