By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
460,028 Members | 863 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 460,028 IT Pros & Developers. It's quick & easy.

Static Pointer to Functions inside Class?

P: n/a
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
Share this Question
Share on Google+
1 Reply


P: n/a
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.