Skip to content

Instantly share code, notes, and snippets.

@jeandudey
Last active August 29, 2015 14:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jeandudey/b1f9c11265e944f4877e to your computer and use it in GitHub Desktop.
Save jeandudey/b1f9c11265e944f4877e to your computer and use it in GitHub Desktop.
Can i wrap argc and argv to an object in C++?

Can i wrap argc and argv to an object in C++?

Lets say you're a purist object oriented programmer and you want wrap the argc and argv constant variables in a more situable container like std::vector and don't know how?

Here is the pre-canned solution just for you (licensed under the WTFPL):

// File: ArgumentsList.hpp
typedef std::vector<std::string> ArgumentsList;

void fill_arguments(ArgumentsList &args, int argc, const char **argv)
{
    for (int i = 0; i < argc; i++)
        args[i] = std::string(argv[i]);
}

And use it like this:

#include "ArgumentsList.hpp"

void print_arguments(const ArgumentsList &arguments)
{
    for (int i = 0; i < arguments.size(); i++)
        std::cout << "arguments[" << i << "]: " << arguments[i] << std::endl;
}

int main(int argc, const char **argv)
{
    ArgumentsList arguments(argc);

    fill_arguments(arguments, argc, argv);

    print_arguments(arguments);

    return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment