Solved

Enum Functions

Posted on 2002-06-06
6
306 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
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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

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…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
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.

792 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