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:
    > 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.


    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

    On Thursday, June 12, 2014 11:21:37 AM UTC-4, Wouter van Ooijen wrote:

    > 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?


    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. On 6/12/2014 11:54 AM, Matt Gregory wrote:
    > On Thursday, June 12, 2014 11:21:37 AM UTC-4, Wouter van Ooijen wrote:
    >
    >> 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?

    >
    > 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???

    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
    }

    > From what I remember about C++, it can't be done. But maybe a newer

    standard makes it possible nowadays?

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

    V
    --
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Jun 12, 2014
    #4
  5. Matt Gregory

    Matt Gregory Guest

    On Thursday, June 12, 2014 12:20:00 PM UTC-4, Victor Bazarov wrote:
    > On 6/12/2014 11:54 AM, Matt Gregory wrote:
    >
    > > On Thursday, June 12, 2014 11:21:37 AM UTC-4, Wouter van Ooijen wrote:

    >
    > >

    >
    > >> 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?

    >
    > >

    >
    > > 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???
    >
    >
    >
    > 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
    >
    > }
    >
    >
    >
    > > From what I remember about C++, it can't be done. But maybe a newer

    >
    > standard makes it possible nowadays?
    >
    >
    >
    > What can't be done? Be a bit more specific.



    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

    On Thursday, June 12, 2014 12:18:21 PM UTC-4, Juha Nieminen wrote:
    > Matt Gregory wrote:
    >
    > > 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.

    >
    >
    >
    > The two functions having different names is a problem because...?



    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

    On Thursday, 12 June 2014 19:44:17 UTC+3, Matt Gregory wrote:
    > On Thursday, June 12, 2014 12:18:21 PM UTC-4, Juha Nieminen wrote:
    > > Matt Gregory wrote:
    > >
    > > > 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.

    > >
    > >
    > > The two functions having different names is a problem because...?

    >
    >
    > 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).


    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

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ersin Gençtürk
    Replies:
    1
    Views:
    4,356
    Ersin Gençtürk
    Oct 6, 2004
  2. ANoobee
    Replies:
    3
    Views:
    583
  3. Yingjie Lan
    Replies:
    4
    Views:
    653
    John Nagle
    Jan 29, 2010
  4. David Thielen

    making permissions optional

    David Thielen, Nov 20, 2006, in forum: ASP .Net Security
    Replies:
    1
    Views:
    477
    Walter Wang [MSFT]
    Nov 21, 2006
  5. Bryan
    Replies:
    0
    Views:
    281
    Bryan
    Aug 4, 2005
  6. aidy
    Replies:
    5
    Views:
    373
    Trans
    Jun 5, 2006
  7. Richard

    Making a perl module optional?

    Richard, Aug 23, 2005, in forum: Perl Misc
    Replies:
    3
    Views:
    189
    Anno Siegel
    Sep 1, 2005
  8. making a carousel optional?

    , Apr 29, 2011, in forum: Javascript
    Replies:
    11
    Views:
    468
Loading...