Suppose I have a class which has many data members of different type, and maybe add more in future
Class A
{
public:
int getA();
void setA(int a);
...
private:
int m_a;
int m_b;
double m_c;
string m_d;
CustomerType m_e;
char * m_f;
...
}
the problem is: each time I add a another data member, I need to add get/set function. For some reason I cannot change them to public.
one solution is to use getType/setType function together with template:
Class A
{
public:
int getInt(int id){
switch(id)
case ID_A:
return m_a;
case ID_B:
return m_b;
...
}
void setInt(int id,int i){...}
double getDouble(){...}
void setDouble(int id,double d){...}
...
template<T>
T get();
template<> //specialize
double get<double>(){return getDouble();}
...
private:
}
Is there any better solution? Thanks.
Here's a strategy that works for me.