I am currently viewing code that has been ported and does not compile. The code was written more like "C" and passes pointers to functions to set specific mutators on an object. A running object is declared as follows:
class Person
{
std::string n_;
int a_;
public:
void name( const std::string& n ) { n_ = n; }
std::string name() const { return n_; }
void age( const int& a ) { a_ = a; }
int age() const { return a_; }
};
Pretty standard stuff. Then we have some interesting functions that I shortened for brevity:
typedef void (Person::FnSetStr)(const std::string& s);
typedef void (Person::FnSetInt)(const int& i);
void setMem( const std::string& label, Person* person, FnSetStr fn)
{
(person->*fn)(val_s);
}
void setMem( const std::string& label, Person* person, FnSetInt fn)
{
(person->*fn)(val_i);
}
And then it is called as follows:
Person* person = new Person;
setMem("Name", person, Person::name );
setMem("Age", person, Person::age );
The idea is to pass the label, object and address of the corresponding mutator. The type of the third parameter is used to force the compiler to choose which overload for the call and the specific overload then receives the appropriate variable, and calls the function that passes it as a parameter to set the value for the object.
Solaris. , GCC, (1) (2):
error: no matching function for call to
'setMem( const std::string& label, Person* person, <unknown type> )'
, , , Person::age , . , , .
, , (.. , ), , Person .