Solved

Enum Functions

Posted on 2002-06-06
6
303 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
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.

937 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

4 Experts available now in Live!

Get 1:1 Help Now