Solved

Enum Functions

Posted on 2002-06-06
6
302 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
6 Comments
 
LVL 22

Accepted Solution

by:
nietod earned 25 total points
Comment Utility
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
Comment Utility
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
Comment Utility
FYI:
With above method all the types need to have the same return type and accept the same argument type(s).
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 22

Expert Comment

by:nietod
Comment Utility
>> 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
Comment Utility
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
Comment Utility
Force accepted

** Mindphaser - Community Support Moderator **

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

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

771 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now