Help with default parameter value using ptr-to-functor

This is a discussion on Help with default parameter value using ptr-to-functor within the C++ Programming forums, part of the General Programming Boards category; I don't know if this is even possible, but can you set a default function pointer in a function's calling ...

  1. #1
    Registered User
    Join Date
    Jun 2003
    Posts
    41

    Help with default parameter value using ptr-to-functor

    I don't know if this is even possible, but can you set a default function pointer in a function's calling signature, like
    you can for passing ints or floats, so one doesn't need to be passed all the time? If so -- how? I'm having trouble with
    the syntax.

    Below is my calling procedure, which you can see I pass a functor pointer, but I'd like to set a default so I don't need to
    pass a functor pointer every time. This is because most of the time I should just use a default function ptr.

    The errors I get are:
    1) invalid use of member 'MyTable:aram' -- this is a private data member of MyTable: why can't it be used here?
    2) 'parameterNumber' was not declared in this scope: it's on the stack already isn't it? why can't it be used??



    Code:
    Boolean MyTable::GetData(int beginTimestamp, int endTimestamp, string & output, const uint parameterNumber, const uint dataType)
    {
       switch (dataType)
       {
          case 1:
          {
             //create our functor objects
             AbstractFunctor* functorPtrLow = new T_SpecificFunctor<MyParameter> (param[parameterNumber], &MyParameter::GetLowerROCGraphBoundary);
          
             //use the specific function ptr I tell it to use....
             Boolean returnvalue = GetData(beginTimestamp, endTimestamp, output, parameterNumber, &MyTable::GetRateOfChange, functorPtrLow);
                    
             delete functorPtrLow;
             functorPtrLow = NULL;
             
             return (returnvalue);
                             
          }; //end case 
          
     
          case 2:
          {
             //use "default" function ptr.....................................................................no 6th parameters passed here
             Boolean returnvalue = GetData(beginTimestamp, endTimestamp, output, parameterNumber, &MyTable::GetStandardDeviation);
             
             return (returnvalue);                         
                       
          }; //end case      
                
          .......
       } //end switch
    } //end MyTable::GetData(...)


    This is my actual method signature, and I've tried various ways, but I can't get the default parameter syntax right.


    Code:
       protected:
          virtual Boolean MyTable::GetData(int beginTimestamp, int endTimestamp, string & output, const uint parameterNumber, 
                                           Boolean (MyTable::* QAMethod)(float & value, const int timestamp, const uint param_number),
                                           AbstractFunctor*  lowFcn = T_SpecificFunctor<MyParameter> (param[parameterNumber], &MyParameter::DefaultFunction));
    I could do a "if ptr is NULL, use this function" in GetData(...), then pass NULL ptrs, but I was hoping there'd be a more elegant way....

    Thanks for any help....

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,600
    You can't use member variables, index arrays, and so forth in a function parameter. Your best bet would be to have a non-templated base-class that has a default constructor, and then model the function something like:


    Code:
    virtual Boolean 
     MyTable::
      GetData(
        int beginTimestamp, 
        int endTimestamp, 
        string & output, 
        const uint parameterNumber, 
        Boolean (MyTable::* QAMethod)
           (float & value, 
            const int timestamp, 
            const uint param_number),
        const AbstractFunctorBase &  lowFcn 
        = AbstractFunctorBase())
    {
     //
    }
    Of course, since you seem to want to use some default object that depends on the internals of the intantiating object, that may not work for you. In that case, just use the pointer method and make the default value NULL (the 'undesired' method you described.
    Code:
    if( numeric_limits< byte >::digits != bits_per_byte )
        error( "program requires bits_per_byte-bit bytes" );
    24bbs.cpp

  3. #3
    Registered User
    Join Date
    Jun 2003
    Posts
    41
    Thanks for the help. The base class of my functor is non-templated, but the nature of the code won't let me setup a default constructor to solve this problem (like you suspected), so I think I'll go with the NULL pointer solution. Thanks again!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. IHTMLDocument2 get_body error
    By Adrian++ in forum C++ Programming
    Replies: 3
    Last Post: 12-20-2008, 01:15 PM
  2. Quick question concerning switch statements
    By Shaun32887 in forum C++ Programming
    Replies: 22
    Last Post: 08-02-2008, 11:33 AM
  3. get keyboard and mouse events
    By ratte in forum Linux Programming
    Replies: 10
    Last Post: 11-17-2007, 04:42 PM
  4. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  5. Consequence of const-ifying a function parameter
    By hzmonte in forum C Programming
    Replies: 3
    Last Post: 08-24-2006, 01:28 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21