For example, we have this class:
class Coord
{
  double x;
  double y;
  double z;
public:
  Coord() { x = y = z = 0; }
  void set(double xx, double yy, double zz)
  {
    x = xx;
    y = yy;
    z = zz;
  }
  void set_x(double xx) { x = xx; }
  void set_y(double yy) { y = yy; }
  void set_z(double zz) { z = zz; }
  double get_x() { return x; }
  double get_y() { return y; }
  double get_z() { return z; }
};
On these 7 methods we can set and get x,y and z of a coordinate. I am interested in create less methods set() and get() where I can call something like that:
int main()
{
  Coord c;
  c.set_x(5); /* only set x */
  c.set_y(6); /* or y */
  c.set_z(7); /* or z */
  c.set(1,2,5); /* or setting x, y and z */
  c.get_x(); /* only get x */
  c.get_y(); /* or y */
  c.get_z(); /* or z */
}
                If the Coord class is that simple, it could also be a struct.
Anyway you can write something like:
class Coord
{
public:
  enum xyz {x = 0, y, z};
  Coord() : vec{x, y, z} {}
  template<xyz C> void set(double v) { vec[C] = v; }
  template<xyz C> double get() const { return vec[C]; }
  void set(double xx, double yy, double zz)
  {
    set<Coord::x>(xx);
    set<Coord::y>(yy);
    set<Coord::z>(zz);
  }
private:
  double vec[z + 1];
};
and use the class this way:
Coord c;
c.set<Coord::x>(5); /* only set x */
c.set<Coord::y>(6); /* or y */
c.set<Coord::z>(7); /* or z */
c.set(1,2,5); /* or setting x, y and z */
c.get<Coord::x>(); /* only get x */
c.get<Coord::y>(); /* or y */
c.get<Coord::z>(); /* or z */
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With