I have a class:
Temp.h:
[...]
class Temp{
public:
Temp(vector<vector<double> > vect);
const static Temp VAR1;
const static Temp VAR2;
...
};
I want to initialize VAR1 and VAR2 using array of doubles: {{0,1,0,1},{1,0,1,0}}. But I cannot do this like this:
const Temp::VAR1(...);
, because there is no 'one-line' operator to construct vector of vectors from an array of arrays. I thought about building structure, like:
B{
vector<vector<double> > vect;
B& add (double[] a, int number) {...
push elements from array to vector and add this vector to vect ...
return *this;
}
Temp build() {return Temp(vect);}
}
But when I call:
B().add({0,1,0,1},4).add({1,0,1,0},4).build();
Compiler doesn't recognize {0,1,0,1} as an array, but as a double* and I cannot change it.
Is there any option to create some method or something similar to initialize const static member in more lines than one?
I hope I wrote simple enough, if not, I'll edit it later.
In C++11, use the uniform initialisation syntax:
const Temp::VAR1{{0,1,0,1},{1,0,1,0}};
If you have to remain compatible with C++03, I'd recommend using boost::assign:
const Temp::VAR1 = list_of(list_of(0)(1)(0)(1))(list_of(1)(0)(1)(0));
Write a static function that initializes it:
Temp InitTempVar1()
{
vector<vector<double> > init;
// initialize init...
return Temp(init);
}
Temp InitTempVar2()
{
vector<vector<double> > init;
// initialize init...
return Temp(init);
}
const Temp Temp::VAR1 = InitTempVar1();
const Temp Temp::VAR2 = InitTempVar2();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With