Please consider this class:
class A
{
public: //public in this example
string a1;
string a2;
string a3;
string a4;
int a5;
double a6;
(...) plus other 50 member names
multiset<string> getAttrib(const vector(A)& va, string attr)
{
for (vector<string>::const_iterator ci = va.begin; ci != va.end(); ++ci) {
cout << ci->attr << endl; //ERROR
}
};
The member function could be called like:
A a
a.getAttrib(vector_a, "a1");
This results in an error. Const class A has no member named 'attr'.
I didn't want to write 50 different case
statements to achieve the above wanted flexibility.
Is it possible somehow?
And if member names are private can a public member function exist to perform this?
Besides class A
, I have six other similar classes and wanted to have a parent class that would take a vector of any of the seven classes and could return the value (or in the real case a multiset) of the chosen membername.