Question

I'm using a struct of three floats to represent Translation and Scale like this:

typedef struct Translation{
    Translation() : x(0.0), y(0.0), z(0.0){};
    Translation(x, y, z) : x(x), y(y), z(z){};
    //some arithmetic operators
    float x, y, z;
}
Translation;

typedef Translation Scale;

Now I would want to use this definition to represent Rotation as well, but I don't want to access their members as x, y and z, but yaw, pitch and roll. Is it possible to rename members like this via a typedef? Or is there another way which does not include redefining the complete struct and its members?

Was it helpful?

Solution

No, because typedef is for types (e.g. int, double, etc.).

The clearest way is to make another struct.

EDIT: Since this is C++, I suggest you make a base class, that contains all the logic and derive from it. In other words, use inheritance.

Example:

#include <iostream>

class Base {
 public:
  Base(int aa, int bb, int cc)
   : a(aa), b(bb),c(cc) {}

  int getA() {
    return a;
  }

  int getB() {
    return b;
  }

  int getC() {
    return c;
  }

  // Notice that you make the data members private,
  // but then you will need the getters to access them
  // in the derived classes
 protected:
  int a;
  int b;
  int c;
};

class A : public Base {
 public:
  A(int aa, int bb, int cc)
   : Base(aa, bb, cc) {}

  int x() {
    return a;
  }

  int y() {
    return b;
  }

  int z() {
    return c;
  }
};

class B : public Base {
 public:
  B(int aa, int bb, int cc)
   : Base(aa, bb, cc) {}

  int gama() {
    return a;
  }

  int beta() {
    return b;
  }

  int theta() {
    return c;
  }
};

int main() {
  A a_object(1, 2, 3);

  B b_object(4, 5, 6);

  std::cout << "a of a_object = " << a_object.x() << "\n";
  std::cout << "b of b_object = " << b_object.gama() << "\n";

  return 0;
}

OTHER TIPS

So here is my go on this: It is probably not perfect, but allows for easy modification of all three structs at the same time while retaining a clear separation of all three types, which is extremely important as @Oktalist pointed out. There should be no interoperability between the three types, so inheritance and typedef are not an option.

So, I found my relief in Macros:

#define THREE_FLOAT_STRUCT(NAME, X, Y, Z, X_STANDARD, Y_STANDARD, Z_STANDARD) typedef struct NAME{\
    NAME() : X(X_STANDARD), Y(Y_STANDARD), Z(Z_STANDARD){};\
    NAME(float X, float Y, float Z) : X(X), Y(Y), Z(Z){};\
    NAME operator+(const NAME& other) const{ return NAME(X + other.X, Y + other.Y, Z + other.Z); }\
    NAME operator-(const NAME& other) const{ return NAME(X - other.X, Y - other.Y, Z - other.Z); }\
    NAME operator*(float divisor) const{ return NAME(X * divisor, Y * divisor, Z * divisor); }\
    NAME operator/(float divisor) const{ return NAME(X / divisor, Y / divisor, Z / divisor); }\
    NAME& operator+=(const NAME& other){ X += other.X; Y += other.Y; Z += other.Z; return *this; }\
    NAME& operator-=(const NAME& other){ X -= other.X; Y -= other.Y; Z -= other.Z; return *this; }\
    NAME& operator*=(float divisor){ X *= divisor; Y *= divisor; Z *= divisor; return *this; }\
    NAME& operator/=(float factor){ X /= factor; Y /= factor; Z /= factor; return *this; }\
    float X, Y, Z;\
} NAME;

THREE_FLOAT_STRUCT(Rotation, roll, pitch, yaw, 0.0, 0.0, 0.0)
THREE_FLOAT_STRUCT(Translation, x, y, z, 0.0, 0.0, 0.0)
THREE_FLOAT_STRUCT(Scale, x, y, z, 1.0, 1.0, 1.0)

I personally like that solution. It is indeed a bit hard to read, but we're only talking about basic arithmetic operations, the code isn't anything too fancy and you can see on a glance which operators are defined.

If you got better solutions, feel free to post them as another answer.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top