On Mar 6, 10:07 am, Triple-DES <DenPlettf...@[EMAIL PROTECTED]
> wrote:
> On 6 Mar, 17:56, Andrew Falanga <af300...@[EMAIL PROTECTED]
> wrote:
> [snip]
>
>
>
> > However, the main problem that I was faced with is that given my
> > programs needs, the main module of the program calls a series of test
> > functions that are contained in this class. In the main module, to
> > keep that simple, I wanted to use something like this:
>
> > // assume lots of includes and definitions
> > TheClass* tc;
>
> > for(int i(0); i < 25; i++) {
> > tc = new TheClass(i); // the c-tor links the appropriate member
> > function
> > // to the function pointer
> > described below
>
> > // concept only, I know it's improper code
> > tc->CallSingleReference( );
>
> > tcVector.push_back(tc);
>
> > }
>
> > This approach appealed to me much more that a large switch statement,
> > or a series of if/else if's in the code for a different function
> > call. Further, this approach had the scalability for future growth,
> > which is likely given what this program does. Now, I only change
> > functionality in my class.
>
> Let me get this straight: You want to call each member function once
> on a separate instance of the class? If so, why would you need an
> switch?
>
> DP
Not quite. I don't want to call each member function once for each
instance of the class, but rather one and only one function, for each
instance of the class. Further, the function will be a different
function each time. Never the same function twice (or at least that's
what I have to program in safeguards to avoid). Also, command line
options should allow for the selection of certain functions based upon
an index value.
Thus something like this is what I want to avoid:
class sample {
public:
func1() {
// im****tant stuff
}
func2() {
// im****tant stuff
}
};
int main() {
sample* s;
std::vector<sample*>sVector;
for(int i(0); i < 2; i++) {
s = new sample;
switch(i) {
case 1:
s->func1;
break;
case 2:
s->func2;
break;
}
sVector.push_back(s);
}
return 0;
}
Thus my approach is to move the switch, or if/else if, processing to
the constructor for the sample class and then, based upon an index
value passed to the c-tor, assign the function pointer to the proper
member function and then the user of the class simply references the
pointer to member function.
If there is a more elegant way of doing this, please enlighten me.
Andy


|