Functionoids vs Pointer to Member Functions

I

Immortal Nephi

I did read section [33] under C++ FAQs Lite. It talks about pointer
to member functions and functionoids. Someone claims that
functionoids runs faster than traditional pointer to function and
pointer to member functions, but another denies their claim. They say
functionoids can be slow because it requires high overheads through
three pointers. I do not have profiler in front of me because I use
Visual Studio C++ Express Edition. I would appreciate if someone
already tested functionoids and pointer to member functions. They can
tell which is faster.
If I am going to use functionoids instead of pointer to member
functions at compile-time, then all derived classes are unable to
share base class’ data members. The only option is to use static data
member, but I don’t want it.
I create stand alone class. Stand alone class contains data
members. The derived classes are able to access stand alone class
directly. Stand alone class is necessary because I create two
separate functionoid arrays.
Here is an example of my code below.

class R
{
public:
R() : a( 0 ), b( 0 ), c(0 ) {}
~R() {}

int a;
int b;
int c;
};

class Top
{
public:
Top() : data2( 0 ) {}
~Top() {}

virtual void Execute() = 0;

static int data1;
int data2;
};

int Top::data1 = 0;

class A : public Top
{
public:
A( R &_pR ) : pR( &_pR ) {}
~A() {}

virtual void Execute()
{
pR->a += 0x1;
pR->b += 0x2;
pR->c += 0x4;

data1 += 1;
data2 += 1;
}
R* const pR;
};

class B : public Top
{
public:
B( R &_pR ) : pR( &_pR ) {}
~B() {}

virtual void Execute()
{
pR->a += 0x10;
pR->b += 0x20;
pR->c += 0x40;

data1 += 2;
data2 += 2;
}

R* const pR;
};


int main() HINSTANCE h_Instance,
{
R r1;
R r2;

A aa1( r1 );
B bb1( r1 );

A aa2( r2 );
B bb2( r2 );

Top *pTop1[2] = { &aa1, &bb1 };
Top *pTop2[2] = { &aa2, &bb2 };

pTop1[0]->Execute();
pTop1[1]->Execute();

pTop2[0]->Execute();
pTop2[1]->Execute();

int a1 = r1.a;
int a2 = r1.b;
int a3 = r1.c;

int b1 = r2.a;
int b2 = r2.b;
int b3 = r2.c;

system("pause");

return 0;
}
 
J

James Kanze

I did read section [33] under C++ FAQs Lite. It talks about
pointer to member functions and functionoids. Someone claims
that functionoids runs faster than traditional pointer to
function and pointer to member functions, but another denies
their claim. They say functionoids can be slow because it
requires high overheads through three pointers. I do not have
profiler in front of me because I use Visual Studio C++
Express Edition.

That doesn't stop you from writing a quick benchmark.

More generally, however, that benchmark will only tell you which
is faster for the processor you run it on, using the exact
version of the compiler you compiled it with, and the same
options, and for code in the same context. (In general,
however, most implementations of pointer to member function are
fairly slow.)
 
J

James Kanze

If I am going to use functionoids instead of pointer to member
functions at compile-time, then all derived classes are unable
to share base class’ data members. The only option is to use
static data member, but I don’t want it.
Why?

I create stand alone class. Stand alone class contains data
members. The derived classes are able to access stand alone
class directly. Stand alone class is necessary because I
create two separate functionoid arrays.

I'm afraid I don't understand any of this. I more or less
supposed that by functionoids, you meant functional objects, but
I don't see any of those in your code (nor do I see anywhere
where pointers to member functions could be used).
 
I

Immortal Nephi

I'm afraid I don't understand any of this.  I more or less
supposed that by functionoids, you meant functional objects, but
I don't see any of those in your code (nor do I see anywhere
where pointers to member functions could be used).

Please read my source code above. You can understand it easily. I do
not have to mention pointer to member functions. I only show
functionoids. Pointer to member functions and data members are in one
base class. Derived classes are not necessary. Pointer to member
functions do not have problems to share data members.

However, if you define two derived classes from base class, then each
derived class has its own copy data member, but they cannot share base
class' data member unless you declare static data members. Stand
alone class is necessary if you define two or more base class
pointer. You can have two or more separate Top class pointer arrays.

For example, first array contains class A and Class B. Second array
contains another class A and class B. You can always choose one of
two arrays before you want to invoke either Top1[0]->Execute() or Top2
[0]->Execute(). Both of them access their own copy of data members
from stand alone class.

They did point that pointer to member functions is slow. How can they
assure that functionoids is the best option. I would have more than
256 derived classes which are derived from Top class and Top class'
array contains address member of each derived class. I can choose one
out of 256 derived class when I want to invoke Execute() function.
All 256 derived classes are able to access stand alone class' data
members instead of using Top class' static data members.

Hopefully, I explain better.
 
S

SG

I did read section [33] under C++ FAQs Lite.  It talks about pointer
to member functions and functionoids.  Someone claims that
functionoids runs faster than traditional pointer to function and
pointer to member functions, but another denies their claim.  They say
functionoids can be slow because it requires high overheads through
three pointers.

It really depends on what you think a "functionoid" is and how this
thing will be used. Also, I don't think a lot of people use the term
"functionoid" which raises the question how comments about
"functionoids" got into this FAQ.

Anyway ... There are multiple examples on that page with varying
runtime/code size trade-offs. What they have in common is that it's
about invoking functions on objects as opposed to using plain function
pointers. (Note: Pointer to member functions are NOT compared to
"functionoids" on that page). One example uses an abstract base class
with a virtual "doit" member function and another uses templates with
function objects that don't have virtual member functions. It's a
completely different thing. Take your pick.
I would appreciate if someone
already tested functionoids and pointer to member functions.
They can tell which is faster.
If I am going to use functionoids instead of pointer to member
functions

What kind of "functionoids"? How would you use them? How would you use
pointers to member functions?
at compile-time, then all derived classes are unable to
share base class’ data members.

Why are they unable?

[snip]

It's probably for the best if you explain the problem you are trying
to solve.

Cheers!
SG
 
J

James Kanze

Please read my source code above. You can understand it
easily. I do not have to mention pointer to member functions.
I only show functionoids. Pointer to member functions and
data members are in one base class. Derived classes are not
necessary. Pointer to member functions do not have problems
to share data members.

I still don't see what you're trying to do. I see what the
actual code does, but I don't know which parts are part of the
requirements, and which parts are only part of the example
implementation, and can be changed.
However, if you define two derived classes from base class,
then each derived class has its own copy data member, but they
cannot share base class' data member unless you declare static
data members. Stand alone class is necessary if you define
two or more base class pointer. You can have two or more
separate Top class pointer arrays.

I'm not sure I understand here. If the data is a member of the
base class, it is a member of all of the derived classes.
Static doesn't play a role. Static causes there to be only one
instance of the data, regardless of the number of instances of
the class.
For example, first array contains class A and Class B.

More exactly, it contains pointers to instances of classes A and
B.
Second array contains another class A and class B. You can
always choose one of two arrays before you want to invoke
either Top1[0]->Execute() or Top2 [0]->Execute(). Both of
them access their own copy of data members from stand alone
class.

I still don't understand what you're trying to accomplish, so I
can't describe a possible solution.

One thing is clear: "functioniods" normally shouldn't contain
mutable data. Their only role is to bridge beween the call site
and the actual object being used. If A and B are supposed to be
functionoids (replacements for pointers to member functions),
then the mutable data should be in the object they use (the
instance of R).
They did point that pointer to member functions is slow. How
can they assure that functionoids is the best option.

Because it is more flexible. Speed has nothing to do with it.
It all depends on what you're doing, but in the past, almost
every time I've started by using pointer to member functions,
I've modified the code later to use functions, because I've
ultimately needed the added flexibility.

Note too that the pointer to member function syntax is often
somewhat confusing to less experienced pointers. Which is
another good reason to avoid it.
I would have more than 256 derived classes which are derived
from Top class and Top class' array contains address member of
each derived class. I can choose one out of 256 derived class
when I want to invoke Execute() function. All 256 derived
classes are able to access stand alone class' data members
instead of using Top class' static data members.

I don't understand that last sentence, but yes, if you have 256
different options, you might need 256 different classes.
Perhaps a class template could be used to help.
 
N

Noah Roberts

SG said:
I did read section [33] under C++ FAQs Lite. It talks about pointer
to member functions and functionoids. Someone claims that
functionoids runs faster than traditional pointer to function and
pointer to member functions, but another denies their claim. They say
functionoids can be slow because it requires high overheads through
three pointers.

It really depends on what you think a "functionoid" is and how this
thing will be used. Also, I don't think a lot of people use the term
"functionoid" which raises the question how comments about
"functionoids" got into this FAQ.

I didn't believe it was in the FAQ; I had to check. :p

This is the first time I've heard this term.
 
J

James Kanze

SG said:
I did read section [33] under C++ FAQs Lite. It talks
about pointer to member functions and functionoids.
Someone claims that functionoids runs faster than
traditional pointer to function and pointer to member
functions, but another denies their claim. They say
functionoids can be slow because it requires high overheads
through three pointers.
It really depends on what you think a "functionoid" is and
how this thing will be used. Also, I don't think a lot of
people use the term "functionoid" which raises the question
how comments about "functionoids" got into this FAQ.
I didn't believe it was in the FAQ; I had to check. :p
This is the first time I've heard this term.

Same here. I originally supposed that he really meant functor,
or functional object, but in fact, the concept is different. I
feel like M. Jourdain---I've been using functionoids for years,
but didn't know it.
 

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
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top