473,326 Members | 2,102 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Pointer to member function

class A
{
A(int mode) {pF = F1;}
void F1() {}
void (*pF)();
};

How do I do something like this?

I need a function that depends on variable 'mode' and will be called
very very often. Thus, implementing a "if (mode)" in the function is
not possible.

The function returns a pointer to a screen pixel and determines
landscape/portrait mode.

--
-Gernot
int main(int argc, char** argv) {printf
("%silto%c%cf%cgl%ssic%ccom%c", "ma", 58, 'g', 64, "ba", 46, 10);}

Jul 23 '05 #1
9 2311
Gernot Frisch wrote:
class A
{
A(int mode) {pF = F1;}
void F1() {}
void (*pF)();
};

How do I do something like this?

I need a function that depends on variable 'mode' and will be called
very very often. Thus, implementing a "if (mode)" in the function is
not possible.

The function returns a pointer to a screen pixel and determines
landscape/portrait mode.


I'm not exactly sure what you're doing.

How about this (using pointer to member syntax).

class A
{
public:
void F1();
void F2();

void F( void ( A::* func )() )
{
(this->*func)();
}
};

int main()
{
A a;

a.F( & A::F1 );
a.F( & A::F2 );
}

Jul 23 '05 #2
Gernot Frisch wrote:

I need a function that depends on variable 'mode' and will be called
very very often. Thus, implementing a "if (mode)" in the function is
not possible.


Be careful here: the code for calling through a pointer to member
function can be quite complex. Try it before you optimize it.

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
Jul 23 '05 #3
Gernot Frisch wrote:
class A
{
A(int mode) {pF = F1;}
void F1() {}
void (*pF)();
};

How do I do something like this?
Read the FAQ, specifically the section on pointer-to-members.
I need a function that depends on variable 'mode' and will be called
very very often. Thus, implementing a "if (mode)" in the function is
not possible.


You mean, you want to exchange the if-statement with an approach
which is even slower? Pointer to [member] functions are the way to
go! You would trade one or two CPU cyles of the if-statement plus
an occasional pipeline stall (when you change between modes the
branch prediction will be confused) against a full blown function
call. The function call will need to put parameters into appriate
registers and possibly save and later restore other registers.

If you really want to avoid the overhead, you might want to use
a template for your code which is parameterized e.g. with pointer
to members selecting the appropriate member variable. In any case,
I doubt that you will get better performance using a pointer to
[member] function than the if-statement if you have just two
choices.
--
<mailto:di***********@yahoo.com> <http://www.dietmar-kuehl.de/>
<http://www.contendix.com> - Software Development & Consulting

Jul 23 '05 #4
Gianni Mariani schrieb:
a.F( & A::F1 );
a.F( & A::F2 );


Why are there the &s?
Jul 23 '05 #5
Michael Etscheid wrote:
Gianni Mariani schrieb:
a.F( & A::F1 );
a.F( & A::F2 );

Why are there the &s?


Because it's an error otherwise. When converting to "pointer to member"
funtion, the "&" is required. This is not so for non-member funtions.
Jul 23 '05 #6

"Dietmar Kuehl" <di***********@yahoo.com> schrieb im Newsbeitrag
news:11**********************@f14g2000cwb.googlegr oups.com...
Gernot Frisch wrote:
class A
{
A(int mode) {pF = F1;}
void F1() {}
void (*pF)();
};

How do I do something like this?


Read the FAQ, specifically the section on pointer-to-members.
I need a function that depends on variable 'mode' and will be
called
very very often. Thus, implementing a "if (mode)" in the function
is
not possible.


You mean, you want to exchange the if-statement with an approach
which is even slower? Pointer to [member] functions are the way to
go! You would trade one or two CPU cyles of the if-statement plus
an occasional pipeline stall (when you change between modes the
branch prediction will be confused) against a full blown function
call. The function call will need to put parameters into appriate
registers and possibly save and later restore other registers.

If you really want to avoid the overhead, you might want to use
a template for your code which is parameterized e.g. with pointer
to members selecting the appropriate member variable. In any case,
I doubt that you will get better performance using a pointer to
[member] function than the if-statement if you have just two
choices.


OK. What's the fastest way of doing:

void MyBMPClass::Draw()
{
for (x ...)
for (y ...)

switch(mode)
{
case 0:
Func0(x,y);
break;
case 1:
Func1(x,y);
break;
...
}
}


Jul 23 '05 #7
Gernot Frisch schrieb:
OK. What's the fastest way of doing:

void MyBMPClass::Draw()
{
for (x ...)
for (y ...)

switch(mode)
{
case 0:
Func0(x,y);
break;
case 1:
Func1(x,y);
break;
...
}
}


It might be this:

typedef void (*fp)(int, int);
fp arr[100] = { Func0, Func1, ... Func99 };

void MyBMPClass:Draw()
{
arr[mode];
}
Jul 23 '05 #8
Gernot Frisch schrieb:
OK. What's the fastest way of doing:

void MyBMPClass::Draw()
{
for (x ...)
for (y ...)

switch(mode)
{
case 0:
Func0(x,y);
break;
case 1:
Func1(x,y);
break;
...
}
}


It might be this:

typedef void (*fp)(int, int);
fp arr[100] = { Func0, Func1, ... Func99 };

void MyBMPClass::Draw()
{
// create x and y

arr[mode](x, y);
}
Jul 23 '05 #9
Gernot Frisch wrote:
OK. What's the fastest way of doing:

void MyBMPClass::Draw()
{
for (x ...)
for (y ...)

switch(mode)


I assume that 'mode' is a constant within 'Draw()' and that speed
is your ultimate goal, i.e. you would accept a reasonable increase
in code size. The approach using a switch statement within the
inner loop is probably better than using a function pointer due to
the function call overhead (as noted in my previous article or by
Pete Becker). If 'mode' is indeed constant within your function,
you would get still better performance using something like

switch (mode)
{
case 0:
for (x ...)
for (y ...)
Func0(x, y);
break;
case 1:
for (x ...)
for (y ...)
Func1(x, y);
break;
...
}

Of course, you typically don't want to repeat the code, especially
if it is even more than just the two nested loops. In this case,
you might try to do it like this:
template <void (*func)(int, int)>
void DrawAux()
{
for (x ...)
for (y ...)
func(x, y);
}

void Draw(int mode)
{
switch (mode)
{
case 0: DrawAux<Func0>(); break;
case 1: DrawAux<Func1>(); break;
...
}
}

Whether the compiler optimizes this code to avoid the function call
is, of course, depending on the compiler. You will need to profile
this.

BTW, rather than using a switch statement in the above, you might
also try to use a virtual function: the virtual function dispatch
in the inner loop may be too expensive but putting the loop into
the function may well warrant the virtual function.
--
<mailto:di***********@yahoo.com> <http://www.dietmar-kuehl.de/>
<http://www.contendix.com> - Software Development & Consulting

Jul 23 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: David Hill | last post by:
Hello - I am using a library that takes a function pointer as an argument. Is the code below not possible? int library_func(void (*func)(int, short, void *)); I am trying to do this... ...
5
by: Newsgroup - Ann | last post by:
Gurus, I have the following implementation of a member function: class A { // ... virtual double func(double v); void caller(int i, int j, double (* callee)(double)); void foo() {caller(1,...
37
by: Ben | last post by:
Hi, there. Recently I was working on a problem where we want to save generic closures in a data structure (a vector). The closure should work for any data type and any method with pre-defined...
7
by: jon wayne | last post by:
Hi I'm a little confused here about the lifetime of a static pointer to member function, Say, I declare,define & initialize a static ptr to mem function in the header file of a class(the class...
6
by: keepyourstupidspam | last post by:
Hi, I want to pass a function pointer that is a class member. This is the fn I want to pass the function pointer into: int Scheduler::Add(const unsigned long timeout, void* pFunction, void*...
7
by: WaterWalk | last post by:
Hello. I thought I understood member function pointers, but in fact I don't. Consider the following example: class Base { public: virtual ~Base() {} }; class Derived : public Base {
5
by: Tim Frink | last post by:
Hi, I'm experimenting with function pointers and found two questions. Let's assume this code: 1 #include <iostream> 2 class A; 3 4 //////////////////////////////////////////// 5 class B
5
by: Immortal Nephi | last post by:
I would like to design an object using class. How can this class contain 10 member functions. Put 10 member functions into member function pointer array. One member function uses switch to call...
4
by: Immortal_Nephi | last post by:
I had a lot of research to see how function pointer works. Sometimes, programmers choose switch keyword and function in each case block can be called. Sometimes, they choose ordinary function...
7
by: ghulands | last post by:
I am having trouble implementing some function pointer stuff in c++ An object can register itself for many events void addEventListener(CFObject *target, CFEventHandler callback, uint8_t...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.