Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Enum Functions

Posted on 2002-06-06
6
Medium Priority
?
313 Views
Last Modified: 2010-04-02
hi All,
i have a C++ generic class (not MFC)
i have a bunch of functions, and would like to know
how can i Enumerate those function
so the caller won't need to call each one of the explicitly.

thanks
Shay
0
Comment
Question by:Shay050799
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 22

Accepted Solution

by:
nietod earned 100 total points
ID: 7060208
What do you mean!

There is no built-in way to find out all the functions in a class--nor would you really want to call them all either.  (amoung the functions inside a class are destructors, and constructors, and you would not want to call them at "random" times)

You can provide a single function in the class that calls the other functions that you want to have called, like

Class A
{
pyblic:
    void F1();
    void F2();
    void F3();
    void F4();

    void CallAll()
    {
       F1();
       F2();
       F3();
       F4();
    }
};


But you won't find any automated way of doing this.   Like what would you do if some functions required parameters?  ie. what about

class A
{
public:
    void  F1(int i);
    void F2(double d, int i);
};

How could you call both of those functions from one "general" mechanism?


Maybe it would be best if you explained your cicumstances and what you need to accomplish, then we can try to suggest a better means fo getting there.  I think your current approach is a deadend.
0
 
LVL 30

Expert Comment

by:Axter
ID: 7060296
I'm not sure if this is the kind of thing your looking for.
The following method allows you to store an array of member functions, and them by iterating through the array.
You can also use a vector to store the member functions.

#include <stdlib.h>

class foo
{
public:
     int g1(){return 1;}
     int g2(){return 2;}
     int g3(){return 3;}
     int g4(){return 4;}
     int g5(){return 5;}
     int g6(){return 6;}
};

int main(int argc, char* argv[])
{
     typedef int (foo::*FuncPtr)();
     FuncPtr ListOfFunctions[6] = {foo::g1, foo::g2, foo::g3, foo::g4, foo::g5, foo::g6};

     for (int i = 0;i < 6;++i)
     {
          foo MyFoo;

          printf("%i = %i\n", i, (MyFoo.*ListOfFunctions[i])());
     }
     
     system("pause");
     return 0;
}

0
 
LVL 30

Expert Comment

by:Axter
ID: 7060299
FYI:
With above method all the types need to have the same return type and accept the same argument type(s).
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 22

Expert Comment

by:nietod
ID: 7060319
>> With above method all the types need to have the same return type
>> and accept the same argument type(s).
in other words, the functions all need to have the same "signature"  which includes calling convention as well.

It would really help if we knew what this is for.
0
 
LVL 11

Expert Comment

by:griessh
ID: 7261534
Dear Shay

I think you forgot this question. I will ask Community Support to close it unless you finalize it within 7 days. You can always request to keep this question open. But remember, experts can only help you if you provide feedback to their questions.
Unless there is objection or further activity,  I will suggest to split between

     "Axter & nietod"

comment(s) as an answer.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
======
Werner
0
 
LVL 6

Expert Comment

by:Mindphaser
ID: 7332900
Force accepted

** Mindphaser - Community Support Moderator **

Axter, there will be a separate question with points for your help.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
Suggested Courses

610 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question