Solved

Declare a method with default arguments in C++ ( array of string)

Posted on 2014-11-22
9
141 Views
Last Modified: 2014-11-24
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,
0
Comment
Question by:bachra04
9 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 40459561
You can't provide a default argument for a 'std::list<std::string>&', since there is no NULL value for references. But why not just providing a 2nd overload for 'setState()' that only takes one argument, e.g.

void  setState (std::string reason, bool bAfter, std::list<std::string>& allStates);

// ...

void  setState (std::string reason) {

  std::list<std::string> empty_list;

  setState(reasob, true, empty_list); // call original 'setState()' with an empty list
}

Open in new window

0
 
LVL 32

Accepted Solution

by:
phoffric earned 500 total points
ID: 40459652
Maybe you can handle a "null" list by checking for an empty list:
std::list<std::string> defaultStates;

void  setState (std::string reason, bool bAfter = false, std::list<std::string>& allStates = defaultStates) {
   std::cout << reason << " bAfter = " << bAfter;
   if( !allStates.empty() )
   {
      std::cout << "  " << allStates.back();
      std::cout << "  " << allStates.front();
   }
   std::cout << std::endl;
}

int main() {
   defaultStates.push_back ("ON");
   defaultStates.push_back ("OFF");
   setState("default");
   defaultStates.push_front("the end");
   setState("actual ", true, defaultStates);
}

Open in new window

0
 
LVL 86

Expert Comment

by:jkr
ID: 40459710
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
}
                                          

Open in new window

0
 
LVL 86

Expert Comment

by:jkr
ID: 40459757
>>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...
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 32

Expert Comment

by:phoffric
ID: 40459801
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.
0
 
LVL 8

Expert Comment

by:Subrat (C++ windows/Linux)
ID: 40460334
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).
0
 
LVL 32

Expert Comment

by:phoffric
ID: 40460337
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.
0
 
LVL 33

Expert Comment

by:sarabande
ID: 40461880
the following compiles without problems:

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);
    }
}

Open in new window

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;
     }
     ...
};

Open in new window

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);
    }
}

Open in new window


Sara
0
 
LVL 2

Author Comment

by:bachra04
ID: 40462306
Thanks Sara;

It is also a nice suggestion.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

932 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now