471,892 Members | 1,904 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,892 software developers and data experts.

Static Pointer to Functions inside Class?

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();

}

--

Yours Truly,
Bryan Parkoff
Dec 15 '07 #1
1 1382
Bryan Parkoff wrote:
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.

--
Ian Collins.
Dec 15 '07 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by Dumitru Sipos | last post: by
1 post views Thread by Bryan Parkoff | last post: by
8 posts views Thread by Srini | last post: by
9 posts views Thread by Bryan Parkoff | last post: by
14 posts views Thread by Jess | last post: by
10 posts views Thread by Pramod | last post: by
3 posts views Thread by Bryan Parkoff | last post: by
reply views Thread by YellowAndGreen | last post: by
reply views Thread by zermasroor | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.