bachra04
asked on
Declare a method with default arguments in C++ ( array of string)
Hi Experts,
I have the following method :
void setState (std::string reason, bool bAfter, std::list<std::string>& allStates);
Is there a way to declare it with default arguments for bool and std::list<std::string> ?
so that I can call it in some places of my code
setState("shutdown");
Thanks,
I have the following method :
void setState (std::string reason, bool bAfter, std::list<std::string>& allStates);
Is there a way to declare it with default arguments for bool and std::list<std::string> ?
so that I can call it in some places of my code
setState("shutdown");
Thanks,
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
BTW, as a side note - you might want to consider passing the 'reason' parameter as a 'string&', too. That will avoid copying the string at each call. And also consider making it 'const'. E.g.
void setState (const std::string reason, bool bAfter, std::list<std::string>& allStates);
// ...
void setState (const std::string& reason) {
std::list<std::string> empty_list;
setState(reasob, true, empty_list); // call original 'setState()' with an empty list
}
>>Maybe you can handle a "null" list by checking for an empty list:
Which means that you still have t pass an empty list that has to be declared and instantiated. Does not make that very elegant to handle...
Which means that you still have t pass an empty list that has to be declared and instantiated. Does not make that very elegant to handle...
I expect that the author has a reason for wanting to pass in a default list. Personally, I rarely use defaults because once you start, it never ends, and then I think, for others, reading the code becomes a little harder in tracking down whether an overload was called or whether a function with default values was called.
In my opinion, just to achieve the default argument feature, we are creating a global list object or might be a data member. Which is totally unnecessary.
Better approach would be the calling 3-arg version from overloaded 1-arg version as told by JKR-(ID: 40459561).
Better approach would be the calling 3-arg version from overloaded 1-arg version as told by JKR-(ID: 40459561).
I would expect that the author already has some object that represents what is desired to be the default value for the preponderance of cases. We are creating a global list object just to show how to address the author's question.
the following compiles without problems:
of course a "global" states list as suggested by phoffric makes more sense. you probably would provide the list by using a static get function like that:
Sara
void setState (std::string reason, bool bAfter = false, std::list<std::string>& allStates = std::list<std::string>())
{
if (std::find(allStates.begin(), allStates.end(), reason) != allStates.end())
{
allStates.push_back(reason);
}
}
so if you want a "default" which does nothing you can do it without defining a global list.of course a "global" states list as suggested by phoffric makes more sense. you probably would provide the list by using a static get function like that:
class Global // or use any other class ...
{
static std::list<std::string> & getAllStates()
{
static std::list<std::string> allStates;
return allStates;
}
...
};
you could use it like:void setState (std::string reason, bool bAfter = false, std::list<std::string>& allStates = std::list<std::string>())
{
if (allStates.empty() == true)
{
allStates = Global::getAllStates();
}
if (std::find(allStates.begin(), allStates.end(), reason) != allStates.end())
{
allStates.push_back(reason);
}
}
Sara
ASKER
Thanks Sara;
It is also a nice suggestion.
It is also a nice suggestion.
Open in new window