jeudi 30 mars 2017

In C++, can I specify or select a member of a class ( and use it as parameter )?

For example, let's say I have:

Class AB { float a,b; };

How do I write one function that can process either "a" or "b", based on a parameter that's passed in, in a "proper" way ? I want to do something like this:

float getSqrt(AB x, ClassMemberSelector s) 
{ return sqrt(x.s); }

AB x;
getSqrt(x, SelectClassMember(AB::a) ); //get square root of x.a
getSqrt(x, SelectClassMember(AB::b) ); //get square root of x.b

Thanks.





Aucun commentaire:

Enregistrer un commentaire