Making class-typed method parameters optional without overloading?

Discussion in 'C++' started by Matt Gregory, Jun 12, 2014.

  1. Matt Gregory

    Matt Gregory Guest

    I have a class method that looks like this:

    double TJDBCustRewardTable::GetTotal(RewardsTotalType totalType, AnsiString CustNum, int CustSubNum, TDateTime StartDate, TDateTime EndDate)

    What would be the best way to make the StartDate and EndDate parameters optional?

    I don't want to use overloading because I want to call this method with a function pointer.

    I would like to declare this like:

    double GetTotal(RewardsTotalType totalType, AnsiString CustNum, int CustSubNum, TDateTime StartDate = NULL, TDateTime EndDate = NULL);

    and then test for NULL in the method body, but I know that won't work.

    Thanks!
    Matt
     
    Matt Gregory, Jun 12, 2014
    #1
    1. Advertisements

  2. Matt Gregory schreef op 12-Jun-14 4:53 PM:
    If TDateTime is a pointer type that will work!

    Maybe think like this: if the user doesn't supply StartDate and EndDate,
    what dates would you use? If fixed dates, you can supply those as
    defaults. If not, can you think of some impossible date value that you
    can specify as default and test for in the body?

    Wouter van Ooijen
     
    Wouter van Ooijen, Jun 12, 2014
    #2
    1. Advertisements

  3. Matt Gregory

    Matt Gregory Guest

    I thought I might define a const TDateTime NODATE; constant in the class, so the caller could use TJDBCustRewardTable::NODATE in order to bypass those parameters, but if TDateTime requires a constructor???

    From what I remember about C++, it can't be done. But maybe a newer standard makes it possible nowadays?

    Matt
     
    Matt Gregory, Jun 12, 2014
    #3
  4. class, so the caller could use TJDBCustRewardTable::NODATE in order to
    bypass those parameters, but if TDateTime requires a constructor???

    class Example1
    {
    public:
    Example1(int value);
    static const Example1 NOTHING;
    };

    const Example1 Example1::NOTHING(42);

    void foo(Example1 something = Example1::NOTHING)
    {
    if (something == Example1::NOTHING) ... // requires op== defined
    }

    int main()
    {
    foo(Example1(73));
    foo(); // using default value
    }
    standard makes it possible nowadays?

    What can't be done? Be a bit more specific.

    V
     
    Victor Bazarov, Jun 12, 2014
    #4
  5. Matt Gregory

    Matt Gregory Guest


    I stand corrected. Thank you! I didn't how to do it.

    Matt
     
    Matt Gregory, Jun 12, 2014
    #5
  6. Matt Gregory

    Matt Gregory Guest


    Just because I want to pass around one function pointer rather than two. If the function is overloaded, then I would need a function pointer for every method in the overload group (or whatever you call it).

    Matt
     
    Matt Gregory, Jun 12, 2014
    #6
  7. Matt Gregory

    Öö Tiib Guest

    Default parameter is not part of type of function so function pointer does not
    have default parameters and so it does not carry them.

    You can wrap function pointer with function that has default parameters
    or wrap it with small class that has operator() with default parameters or has
    operator() overloads or wrap it with lambda and so on. IOW there are plenty of
    solutions to the very problem but plain function pointer is too few to solve it.
     
    Öö Tiib, Jun 13, 2014
    #7
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.