Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > C - C++ Learning > Re: Still havin...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 10 of 14 Topic 4050 of 4302
Post > Topic >>

Re: Still having problems with my member function pointers

by Andrew Falanga <af300wsm@[EMAIL PROTECTED] > Mar 6, 2008 at 10:04 AM

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
 




 14 Posts in Topic:
Still having problems with my member function pointers
Andrew Falanga <af300w  2008-03-06 07:45:14 
Re: Still having problems with my member function pointers
"Alf P. Steinbach&qu  2008-03-06 17:27:18 
Re: Still having problems with my member function pointers
Triple-DES <DenPlettfr  2008-03-06 08:39:26 
Re: Still having problems with my member function pointers
Andrew Falanga <af300w  2008-03-06 08:41:02 
Re: Still having problems with my member function pointers
"Alf P. Steinbach&qu  2008-03-06 17:49:40 
Re: Still having problems with my member function pointers
Andrew Falanga <af300w  2008-03-06 08:56:09 
Re: Still having problems with my member function pointers
"Alf P. Steinbach&qu  2008-03-06 19:19:36 
Re: Still having problems with my member function pointers
Triple-DES <DenPlettfr  2008-03-06 09:07:53 
Re: Still having problems with my member function pointers
Bart van Ingen Schenau &l  2008-03-06 18:49:13 
Re: Still having problems with my member function pointers
Andrew Falanga <af300w  2008-03-06 10:04:26 
Re: Still having problems with my member function pointers
Triple-DES <DenPlettfr  2008-03-06 10:57:45 
Re: Still having problems with my member function pointers
Andrew Falanga <af300w  2008-03-06 11:43:17 
Re: Still having problems with my member function pointers
Andrew Falanga <af300w  2008-03-06 11:47:30 
Re: Still having problems with my member function pointers
Triple-DES <DenPlettfr  2008-03-06 22:03:46 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Mon Oct 13 20:11:59 CDT 2008.