New C++ question: Functions?

Discussion in 'OT Technology' started by Joe_Cool, Oct 13, 2004.

  1. Joe_Cool

    Joe_Cool Never trust a woman or a government. Moderator

    Joined:
    Jun 30, 2003
    Messages:
    299,308
    Likes Received:
    563
    In languages like Perl and PHP, you can do things like this:

    function blah()
    {
    ...
    }

    function bar()
    {
    ...
    }

    foo = "blah";
    $foo();

    foo = "bar";
    $foo();

    with the result being that the function name stored as $foo is executed: The first $foo() calls blah() and the second calls bar().

    Is there an equivalent functionality in C++? I need to run 7 functions all the same way, and I'd like to do it in a loop if possible.
     
  2. Joe_Cool

    Joe_Cool Never trust a woman or a government. Moderator

    Joined:
    Jun 30, 2003
    Messages:
    299,308
    Likes Received:
    563
    I'm thinking maybe pointer-to-member-function? But how would I go about doing this?

    I want an array of pointers, I guess, where each pointer points to class::function(), or something, right?

    I have this:
    class blah
    {
    int a() { ... }

    int b() { ... }

    int c() { ... }

    int d() { ... }

    int e() { ... }

    int f() { ... }

    int g() { ... }
    };

    and I want to do something like this:

    blah x;

    for(int n=0;n<7;n++)
    { a[n](); }

    where a[0] holds & x.a(),
    a[1] holds & x.b() ...
    and a[6] is & x.g().

    Can it be done?
     
  3. SL1200MK4

    SL1200MK4 New Member

    Joined:
    Sep 27, 2003
    Messages:
    1,552
    Likes Received:
    0
    No idea what you are trying to do...

    Can you be more specific as to what is the intend to do this, and what excatly is the problem. If it's for school assignment, then are you guys talking about design patterns? If so, I think there's design pattern that does something similar to what you did in the Perl example.
     
  4. skinjob

    skinjob Active Member

    Joined:
    Jan 6, 2001
    Messages:
    2,337
    Likes Received:
    0
    Location:
    Aztlán
    You can do it with function pointers in C/C++.

    int a() {...}
    int b() {...}
    int c() {...}

    int (*fp[3])(); // declares an array of 3 function pointers

    /*
    The signature of a function pointer should match
    the signature of the functions you with to point to.

    <return type> (*pointername)(<parameter types>);
    */

    fp[0] = a;
    fp[1] = b;
    fp[2] = c;

    for (int i = 0; i < 3; ++i)
    {
    fp();
    }
     
  5. Joe_Cool

    Joe_Cool Never trust a woman or a government. Moderator

    Joined:
    Jun 30, 2003
    Messages:
    299,308
    Likes Received:
    563
    D'OH. Nevermind. I figured it out. The hard part was figuring out what type to make it. :o

    Edit:

    Thanks guys.
    Skinjob, that's pretty much what I ended up doing.
     
  6. Joe_Cool

    Joe_Cool Never trust a woman or a government. Moderator

    Joined:
    Jun 30, 2003
    Messages:
    299,308
    Likes Received:
    563
    By the way, who's in your av? Looks almost like Spectreman. :eek3:
     
  7. korrupshun

    korrupshun New Member

    Joined:
    Sep 8, 2004
    Messages:
    389
    Likes Received:
    0
    Location:
    YourMommasHouse
    i believe you can do this with generic variable types and templates ie.

    Template foo ( type T, type T2) {...}

    been a while since i wrote in C/C++ so i might be a little off
     
  8. skinjob

    skinjob Active Member

    Joined:
    Jan 6, 2001
    Messages:
    2,337
    Likes Received:
    0
    Location:
    Aztlán
    But, in order for function pointers outside of the class scope to work with member functions, the member functions should be public and static.
     
  9. Joe_Cool

    Joe_Cool Never trust a woman or a government. Moderator

    Joined:
    Jun 30, 2003
    Messages:
    299,308
    Likes Received:
    563
    This is the gist of it:

    int (Sort::*f[7])(unsigned int *);
    int i;
    Sort * s;
    s=new Sort;

    f[0]= &Sort::Bubble;
    f[1]= &Sort::BiDirectionalBubble;
    f[2]= &Sort::Insertion;
    f[3]= &Sort::Quick;
    f[4]= &Sort::Selection;
    f[5]= &Sort::Shaker;
    f[6]= &Sort::Shell;

    for(i=0;i<7;i++)
    {
    (s->*f)(nums);
    }

    Overall, it's easier to do in PHP. :o
     
  10. WERUreo

    WERUreo Imua!

    Joined:
    Oct 15, 2003
    Messages:
    566
    Likes Received:
    0
    Location:
    Daytona Beach, Florida
    Isn't that Ultraman? I used to watch that as a kid all the time...

    EDIT: I could have sworn there used to be an official web site for Ultraman, but damned if I can find it... This is the closest one I found. http://www.ultramantoys.com/

    P.S. Sorry to hijack the thread...
     

Share This Page