c++ - How can I build this typ of class interface -
i interested in trying create custom type , access members using dot semantics. example:
class a{ //simplified, omitting constructors , other methods private: float numbers[3]; public: float x(){ return numbers[0]; } float y(){ return numbers[1]; } float z(){ return numbers[2]; } } so can this:
a; //do stuff populate `numbers` float x=a.x; but make elements in numbers lvalues can this:
a; a.y=5; //assigns 5 numbers[1] how can setting method?
you can return reference allow assignment:
float & x(){ return numbers[0]; } ^ // usage a; a.x() = 42; you should have const overload, allow read-only access const object:
float x() const {return numbers[0];} ^^^^^ // usage const = something(); float x = a.x();
Comments
Post a Comment