Assume proper headers are included.
I have a class for various functions of a calculator. I have several structs for different operator types (nullary, Unirary, Binary, Ternary). I want to initialize that a vector(or preferably an array) of the structs with the elements filled in for w/e my calculator supports. For example nul[]={{pi, "pi"}, {ans, "ans"}}; My program will then take a token from the input and search for the appropriate operator, return the index of that operator, and then i'd invoke the proper function pointer. My problem is initializing an array or vector in the constuctor. I'd appreciate it if someone could show me both ways. thank you. Below is my class and unfinished constructor. Also if u can think of a better way for me to write my calculator I'd love to hear it.
//operator class
class Functions{
public:
Functions();
double pi(double x=0);
double answer(double& x);
double abs(double& x);
double pow(double& x);
double add(double& x, double &y) const;
double subtract(double& x, double& y) const;
double multiply(double& x, double& y)const;
double divide(double& x, double& y) const;
double max(double& x, double& y, double& z) const;
double volume(double& x, double& y, double& z) const;
void doc();
bool weird(double &x) const;
unsigned findop0(const string & name);
unsigned findop1(const string & name);
unsigned findop2(const string & name);
unsigned findop3(const string & name);
private:
struct Nul{
double (*nulf)(double & x);
string name;
};
struct Uni{
bool (*unif)( double & result, double x );
string name;
};
struct Bin{
bool (*binf)( double & result, double x, double y );
string name;
};
struct Tern{
bool (*terf)( double & result, double x, double y, double z );
string name;
};
static const unsigned NUL_ELEMENTS = 2;
static const unsigned UNI_ELEMENTS = 2;
static const unsigned BIN_ELEMENTS = 4;
static const unsigned TRI_ELEMENTS = 2;
vector<Nul> nul;
vector<Uni> uni;
vector<Bin> bin;
vector<Tern> tri;
};
Functions::Functions(){
nul
}
push_backin a constructor function.vectorwith C++11. It was harder than it should have been before that.