0

Hi i have an struct like this:

struct float2 {
  float a;
  float b;
};

now i can access members like this:

float2 f;
f.a = 1;
f.b = 2;

i want to access a and b with other alias as well:

float2 f;
f.a = 1;
f.b = 2;

f.x = 3;
f.y = 4;

f.w = 5;
f.h = 6;

f.width = 7;
f.height = 8;

x, w, width must refer too same memory of a and y, h, height must refer to b

i tried 2 ways but one of them cost memory and one cost performance(i'm not sure):

struct float2
{
    float a;
    float b;
    // plan a ->
    float& x;
    float& y;
    float& w;
    float& h;

    float2(float _a, float _b) : a(_a), b(_b), x(a), y(b), w(a), h(b) {}

    // plan b ->
    float& width() {
      return a;
    }
    float& height() {
      return b;
    }
};

is there any compile time way?

thanks.

High
  • 606
  • 1
  • 6
  • 19
  • Why do you need these aliases? What is the actual problem you need to solve? – Some programmer dude Aug 02 '20 at 11:05
  • `float2` is multi purpose, so i need to use them in different part of program with related alias to make it more readable in that part. – High Aug 02 '20 at 11:09
  • 1
    If you need to use a generic type in a specific way, you normally write an *adapter* or a *wrapper* around the generic type. The generic type should be kept as generic as possible, without any code to imply specific usages of it. So in short, this is a design problem. Keep your `float2` structure as generic as possible, and write other classes which uses ("adapt" or "wraps") the `float2` structure for your specific purposes. – Some programmer dude Aug 02 '20 at 11:14
  • thanks, for your advise but, i think templates just make it more complex and i have no idea that template can have different member aliases. (i have no problem with type of my object) – High Aug 02 '20 at 11:19
  • 1
    I'm not talking about templates. Just create a new class like `class specific_class { private: float2 f; public: float width() { return f.a; } float height() { return f.b; } };` – Some programmer dude Aug 02 '20 at 11:23
  • 1
    And unless you're on a severely memory-constrained system, what you're doing is a rather pointless micro-optimization that will tend to make the code harder to read, write, understand and maintain. – Some programmer dude Aug 02 '20 at 11:24
  • thanks man, but sorry i think thats make my code ugly and unreadable for myself. i saw my pattern used in `glsl` we use this feature why not here in cpp. i think working with 1 type is simpler. – High Aug 02 '20 at 11:32

1 Answers1

0

I suggest you that use like this way.

#include <stdio.h>

struct float2 {
    union {
        float a;
        float x;
        float w;
        float width;
    };
    union {
        float b;
        float y;
        float h;
        float height;
    };
};

int main()
{
    float2 var1;

    var1.x = 10.0;
    var1.a = 12.0;
    var1.w = 14.0;

    var1.y = 0.0;
    var1.h = 4.0;
    var1.height = 6.0;

    printf("a = %f x = %f w = %f width = %f\n", var1.a, var1.x, var1.w, var1.width);
    // OUTPUT:  a = 14.000000 x = 14.000000 w = 14.000000 width = 14.000000

    printf("b = %f y = %f h = %f height = %f\n", var1.b, var1.y, var1.h, var1.height);
    // OUTPUT:  b = 6.000000 y = 6.000000 h = 6.000000 height = 6.000000

    return 0;
}
MsiLucifer
  • 611
  • 9
  • 15