L
Luca Cerone
Dear all,
I'm having a few difficulties in declaring a class with a static constant member.
This member is a vector of double that has to be created once during
the execution and shared among all the objects.
After being created it can't be modified during the whole execution of my program.
I write a toy example to show you what I'd like to do (it doesn't work,
but I can't figure out a way to code it well, that's why I'm posting here).
#include<iostream>
#include<vector>
class QuadFun{
public:
QuadFun(double a, double b) ; // a and b will be used to initialize
// the private vector y to evaluate the values (x-a)*(x-b)
// x has to be a vector that is shared among all the object of the class.
private:
static const std::vector<double> x; // this will be a vector of uniformly
//spaced numbers from 0 to 1. The number of points is determined
//by reading a configuration file. in this example I set the
//number manually in the main function.
std::vector<double> y;
}
int main() {
int numPoints=50; // this value is actually read from a configuration file
// here is the problem. I don't want the number of points to be a member
// of the class.
QuadFun f1=QuadFun(0,1);
QuadFun f2=QuadFun(0.5,0.75);
// how can I make the vector x mad of numpoints created when I first create
// an object of class QuadFun?
// how can I make it "dynamic" so that if I change the value of numPoints
// the x vector will be created accordingly? (in an other execution).
return 0 ;
}
Hope I could explain well what I'd like to achieve.
I know this code is WRONG, but I can't understand how to write it
in a good way.
Can you help me?
Thanks a lot in advance,
Cheers, Luca
I'm having a few difficulties in declaring a class with a static constant member.
This member is a vector of double that has to be created once during
the execution and shared among all the objects.
After being created it can't be modified during the whole execution of my program.
I write a toy example to show you what I'd like to do (it doesn't work,
but I can't figure out a way to code it well, that's why I'm posting here).
#include<iostream>
#include<vector>
class QuadFun{
public:
QuadFun(double a, double b) ; // a and b will be used to initialize
// the private vector y to evaluate the values (x-a)*(x-b)
// x has to be a vector that is shared among all the object of the class.
private:
static const std::vector<double> x; // this will be a vector of uniformly
//spaced numbers from 0 to 1. The number of points is determined
//by reading a configuration file. in this example I set the
//number manually in the main function.
std::vector<double> y;
}
int main() {
int numPoints=50; // this value is actually read from a configuration file
// here is the problem. I don't want the number of points to be a member
// of the class.
QuadFun f1=QuadFun(0,1);
QuadFun f2=QuadFun(0.5,0.75);
// how can I make the vector x mad of numpoints created when I first create
// an object of class QuadFun?
// how can I make it "dynamic" so that if I change the value of numPoints
// the x vector will be created accordingly? (in an other execution).
return 0 ;
}
Hope I could explain well what I'd like to achieve.
I know this code is WRONG, but I can't understand how to write it
in a good way.
Can you help me?
Thanks a lot in advance,
Cheers, Luca