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.
There’s not a direct way to do what you want in C++, although there are some reasonable attempts at reflection frameworks out there. You might also look here: How can I add reflection to a C++ application? for additional commentary about reflection. I would suggest, however, that instead of having 50 member variables, you simply use one private
std::hashmapand usestd::stringkeys. You can then easily pass string parameters along as keys for direct lookups.This, of course, assumes all your 50 members are of the same type, and that may not be suitable for your application. However, take my point, which is: do it a little differently.