Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Functionoids vs Pointer to Member Functions

Reply
Thread Tools

Functionoids vs Pointer to Member Functions

 
 
Immortal Nephi
Guest
Posts: n/a
 
      08-18-2009
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;
}
 
Reply With Quote
 
 
 
 
James Kanze
Guest
Posts: n/a
 
      08-19-2009
On Aug 18, 11:45 pm, Immortal Nephi <(E-Mail Removed)>
wrote:
> 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.)

--
James Kanze (GABI Software) email:(E-Mail Removed)
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
 
Reply With Quote
 
 
 
 
James Kanze
Guest
Posts: n/a
 
      08-19-2009
On Aug 18, 11:45 pm, Immortal Nephi <(E-Mail Removed)>
wrote:
> 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).

--
James Kanze (GABI Software) email:(E-Mail Removed)
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
 
Reply With Quote
 
Immortal Nephi
Guest
Posts: n/a
 
      08-19-2009
On Aug 19, 3:40*am, James Kanze <(E-Mail Removed)> wrote:
> On Aug 18, 11:45 pm, Immortal Nephi <(E-Mail Removed)>
> wrote:
>
> > 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).


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.
 
Reply With Quote
 
SG
Guest
Posts: n/a
 
      08-19-2009
On 18 Aug., 23:45, Immortal Nephi <(E-Mail Removed)> wrote:
> 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
 
Reply With Quote
 
James Kanze
Guest
Posts: n/a
 
      08-20-2009
On Aug 19, 3:32 pm, Immortal Nephi <(E-Mail Removed)> wrote:
> On Aug 19, 3:40 am, James Kanze <(E-Mail Removed)> wrote:
> > On Aug 18, 11:45 pm, Immortal Nephi <(E-Mail Removed)>
> > wrote:


> > > 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).


> 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.

--
James Kanze (GABI Software) email:(E-Mail Removed)
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
 
Reply With Quote
 
Noah Roberts
Guest
Posts: n/a
 
      08-20-2009
SG wrote:
> On 18 Aug., 23:45, Immortal Nephi <(E-Mail Removed)> wrote:
>> 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.

This is the first time I've heard this term.
 
Reply With Quote
 
James Kanze
Guest
Posts: n/a
 
      08-21-2009
On Aug 20, 6:46 pm, Noah Roberts <(E-Mail Removed)> wrote:
> SG wrote:
> > On 18 Aug., 23:45, Immortal Nephi <(E-Mail Removed)> wrote:
> >> 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.


> 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.

--
James Kanze (GABI Software) email:(E-Mail Removed)
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
pointer-to-member data and pointer-to-member functions and access specifiers Stephen Howe C++ 2 11-06-2012 12:32 PM
overloading non-template member functions with template member functions Hicham Mouline C++ 1 04-24-2009 07:47 AM
overloading non-template member functions with template member functions Hicham Mouline C++ 0 04-23-2009 11:42 AM
Functionoids vs Member Function Pointer? Immortal_Nephi@hotmail.com C++ 3 09-14-2008 10:55 AM
pointer to member function and pointer to constant member function Fraser Ross C++ 4 08-14-2004 06:00 PM



Advertisments