I have a class, say C, where one of the member data, say X, depends on user input. The user input may differ on every run, and in my current design, all instances of my classes stores a reference to the same object X.
How can I tweak the design so that it allows default constructor with no arguments?
This way I can use copy assignment/copy constructor, make arrays of C, use temporary rvalue etc.
Below is a minimal working example illustrating my question. In the case I work with, Tag refers to some external resources.
#include <iostream>
#include <string>
#include <vector>
#include <cassert>
using namespace std;
struct Tag {
int N;
string tag;
};
template<typename T>
struct Vec {
const Tag& tag;
T* vec;
Vec(const Tag& tag_) : tag(tag_) {
vec = new T[tag.N];
}
~Vec() {
delete [] vec;
}
};
Tag make_tag(vector<string>& args) {
assert(args.size() == 3);
int N = stoi(args[1]);
return Tag {N, args[2]};
}
vector<string> arguments(int argc, char* argv[]) {
vector<string> res;
for(int i = 0; i < argc; i++)
res.push_back(argv[i]);
return res;
}
int main(int argc, char* argv[]) {
vector<string> args = arguments(argc, argv);
Tag tag0 = make_tag(args);
Tag tag1;
Vec<double> vec(tag0);
return 0;
}