Re: Can I use overloading for optional parameters?
On 30 Mrz., 17:49, DeMarcus <use_my_alias_h...@hotmail.com> wrote:
I need to provide a set of parameters to a function where one or more
can be optional. Standard C++ can only provide default values for the
last parameters and a default value representing N/A for references is
not quite pleasant.
I came up with an idea of overloading the optional parameters. Please
give me your feedback.
class SomeClass
{
public:
void fnc( int& a );
void fnc( int& a, int& b );
void fnc( int& a, int& b, int& c );
// Now insert a pointer if we want to disable (b).
void fnc( int& a, int* b, int& c );
};
Do you mean "Now insert a **NULL** pointer if we want to disable"?
If not this interface would be quite misleading. Why should a pointer
to a valid int not be accepted?
int main()
{
SomeClass sc;
int a, b, c;
// Just want to provide (a).
sc.fnc( a );
// Just want to provide (a) and (b).
sc.fnc( a, b );
// Just want to provide (a) and (c) but not (b).
// Tricky to get right normally, but with this overloading
// it works!
sc.fnc( a, NULL, c );
}
Is this way to provide optional parameters ok, or will I bump into
trouble later I haven't foreseen here?
I cannot answer this question, because the answer depends on
the evolution of your parameters and the question, which
parameter combinations make sense.
My next question is; is there a way to use some technique like traits or
similar to make sure at compile time that (b) for sure is NULL?
Maybe a recent article of Alf
http://groups.google.de/group/comp.lang.c++.moderated/msg/178d5c2061d010f8
gives you an alternative approach?
HTH & Greetings from Bremen,
Daniel Kr?gler
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]