Functionoids vs Pointer to Member Functions

Discussion in 'C++' started by Immortal Nephi, Aug 18, 2009.

  1. 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;
    }
     
    Immortal Nephi, Aug 18, 2009
    #1
    1. Advertising

  2. Immortal Nephi

    James Kanze Guest

    On Aug 18, 11:45 pm, Immortal Nephi <>
    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:
    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
     
    James Kanze, Aug 19, 2009
    #2
    1. Advertising

  3. Immortal Nephi

    James Kanze Guest

    On Aug 18, 11:45 pm, Immortal Nephi <>
    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:
    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
     
    James Kanze, Aug 19, 2009
    #3
  4. On Aug 19, 3:40 am, James Kanze <> wrote:
    > On Aug 18, 11:45 pm, Immortal Nephi <>
    > 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.
     
    Immortal Nephi, Aug 19, 2009
    #4
  5. Immortal Nephi

    SG Guest

    On 18 Aug., 23:45, Immortal Nephi <> 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
     
    SG, Aug 19, 2009
    #5
  6. Immortal Nephi

    James Kanze Guest

    On Aug 19, 3:32 pm, Immortal Nephi <> wrote:
    > On Aug 19, 3:40 am, James Kanze <> wrote:
    > > On Aug 18, 11:45 pm, Immortal Nephi <>
    > > 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:
    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
     
    James Kanze, Aug 20, 2009
    #6
  7. Immortal Nephi

    Noah Roberts Guest

    SG wrote:
    > On 18 Aug., 23:45, Immortal Nephi <> 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. :p

    This is the first time I've heard this term.
     
    Noah Roberts, Aug 20, 2009
    #7
  8. Immortal Nephi

    James Kanze Guest

    On Aug 20, 6:46 pm, Noah Roberts <> wrote:
    > SG wrote:
    > > On 18 Aug., 23:45, Immortal Nephi <> 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. :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.

    --
    James Kanze (GABI Software) email:
    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
     
    James Kanze, Aug 21, 2009
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Fraser Ross
    Replies:
    4
    Views:
    1,094
    Fraser Ross
    Aug 14, 2004
  2. Replies:
    3
    Views:
    485
    James Kanze
    Sep 14, 2008
  3. Hicham Mouline
    Replies:
    0
    Views:
    454
    Hicham Mouline
    Apr 23, 2009
  4. Hicham Mouline
    Replies:
    1
    Views:
    441
    Michael DOUBEZ
    Apr 24, 2009
  5. Stephen Howe
    Replies:
    2
    Views:
    306
    Stephen Howe
    Nov 6, 2012
Loading...

Share This Page