Making class-typed method parameters optional without overloading?

M

Matt Gregory

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
 
W

Wouter van Ooijen

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
 
M

Matt Gregory

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
 
V

Victor Bazarov

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
 
M

Matt Gregory

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.


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

Matt
 
M

Matt Gregory

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
 
Ö

Öö Tiib

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.
 

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. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,812
Latest member
GracielaWa

Latest Threads

Top