Is there a way to assign a class member from a command line argument so that ALL objects of that class have that value (by default)? I want it by default because a member function creates a new object in the Foo class, and I don't want to make Var
a function parameter. I want to avoid global variables if possible. I want my code to do what the code below does, but without the use of global variables.
Foo.h:
#include <string>
#include <iostream>
extern int Var;
class Foo{
public:
int var;
Foo();
};
Foo.cpp:
#include "Foo.h"
Foo::Foo(){
var = Var;
}
main.cpp:
#include "Foo.h"
int Var; // global variable
using namespace std;
int main(int argc, char *argv[]){
Var = stoi(argv[1]);
Foo foo;
cout << foo.var << endl; // should print the command-line argument
}