Hi Iain,
The simplest is to keep your args in a permanent
std::vector<std::string>
, can be a static member of a class or a global variable as:
std::vector<std::string> _arguments;
int main(int argc, char * argv[])
{
_arguments = std::vector<std::string>(argv, argv + argc);
Added after Emilio's comment:
Indeed it starts to migrate your code from C to C++, next step will rewrite your
foo()
function to accept a
std::vector<std::string>&
In between, as Emilio's comment points, you would need an adaptor to use your existing
foo()
, for instance using (VC2010 or gcc 4.5) C++0x lambdas:
#include <vector>
#include <iostream>
#include <algorithm>
#include <iterator>
std::vector<std::string> _arguments;
void foo(int argc, char* argv[])
{
std::copy_n(argv, argc, std::ostream_iterator<char*>(std::cout, " "));
std::cout << std::endl;
}
void CallFoo(std::vector<std::string>& args)
{
std::vector<const char *> argv(args.size());
std::transform(args.begin(), args.end(), argv.begin(), [](std::string& str){
return str.c_str();});
foo(argv.size(), const_cast<char**>(argv.data()));
}
int main(int argc, char * argv[])
{
_arguments = std::vector<std::string>(argv, argv + argc);
CallFoo(_arguments);
return 0;
}
cheers,
AR