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

Popular posts from this blog

php - mySql Join with 4 tables -

css - Text drops down with smaller window -

c# - DetailsView in ASP.Net - How to add another column on the side/add a control in each row? -