2

Could we create a structure that contains some values and the reference that is pointing to the values in the same structure? My idea is to make the alias. So I can call struct members in different way !

struct Size4
{    
    float x, y;
    float z, w;

    float &minX, &maxX, &minY, &maxY;

    Size4(float _x, float _y, float _z, float _w) : x(_x), y(_y), z(_z), w(_w),
        minX(x), maxY(y), minY(z), maxY(w)
    {
    }

};

Thank you all.

NB: I did it with the pointer, but now when I'm trying to call Size4.minX() I'm getting the address, but not the value.

struct Size4
{    
    float x, y;
    float z, w;

    float *minX, *maxX, *minY, *maxY;

    Size4(float _x, float _y, float _z, float _w) : x(_x), y(_y), z(_z), w(_w),
        minX(&x), maxX(&y), minY(&y), maxY(&w)
    {
    }
};
Netherwire
  • 2,669
  • 3
  • 31
  • 54
Dony
  • 95
  • 1
  • 8

2 Answers2

1

"I want to make it transparent. Size4 size(5,5,5,5); size.minX; and size.x; returning the same value..."

You can do it like this. However, I suggest you use a class.

using namespace std;
struct Size4
{
    float x, y;
    float z, w;

    float *minX, *maxX, *minY, *maxY;

    Size4(float _x, float _y, float _z, float _w) : x(_x), y(_y), z(_z), w(_w),
        minX(&x), maxX(&y), minY(&y), maxY(&w)
    {
    }
};

int main() {
  Size4 s(1,2,3,4);
  std::cout << *(s.minX) << std::endl;
  return 0;
}

Or you could add this method in your struct

float getX() {
  return *minX;
}

and access it like this:

std::cout << s.getX() << std::endl;

However, a class would provide better enclosure. Private data members and the get-er function to access the minX.

[EDIT]

Using a class is simple like this:

#include <iostream>

using namespace std;
class Size4
{
 private:
  // these are the private data members of the class
    float x, y;
    float z, w;

    float *minX, *maxX, *minY, *maxY;

 public:
  // these are the public methods of the class
    Size4(float _x, float _y, float _z, float _w) : x(_x), y(_y), z(_z), w(_w),
        minX(&x), maxX(&y), minY(&y), maxY(&w)
    {
    }

    float getX() {
      return *minX;
    }
};

int main() {
  Size4 s(1,2,3,4);
  std::cout << s.getX() << std::endl;
  // std::cout << *(s.minX) << std::endl; <-- error: ‘float* Size4::minX’ is private
  return 0;
}
gsamaras
  • 71,951
  • 46
  • 188
  • 305
  • Your example doesn't show how to use a class, it just rehashes the OP's own pointer use case within the struct. – StoryTeller - Unslander Monica May 18 '14 at 16:47
  • i asked a question near from this one few days earlier : http://stackoverflow.com/questions/23678523/rename-member-by-inherit-class-in-c couldn't i do the same thing in struct ? – Dony May 18 '14 at 16:48
  • @dsimonet I updated my answer with a class usage! Why you want to rename a member of a class? – gsamaras May 18 '14 at 16:54
  • To make my code a be more readable. And because I'm a beginner and some time i want to make simple thinks and lost a lot of time cause i don't know how to do or if it's possible ! – Dony May 18 '14 at 16:57
  • The question you linked has already an answer that you already accepted. I want to help, but I do not know what am I asked. Didn't my answer answered your original question here? @dsimonet – gsamaras May 18 '14 at 16:59
  • Yes your question give me a solution. But not what I'm exactly looking for. No matter. Thanks you a lot for your time :-) – Dony May 18 '14 at 17:00
  • Damn you can not chat, since you have only 5 rep. I will upvote your question so that you can chat in the future. If I can help you more, let me know. :) – gsamaras May 18 '14 at 17:02
0

Take your value using the dereferencing operator: *(size4.minx)

A little example:

Size4 sz(11, 2, 3, 4);
printf("%f, %f, %f, %f", *sz.minX, *sz.maxX, *sz.minY, *sz.maxY);
Netherwire
  • 2,669
  • 3
  • 31
  • 54
  • Thank for your answer, but dereferencing is exactly what I'm avoiding to do. I'm trying to make it transparent. If i can't i'll make class with get and set methods. – Dony May 18 '14 at 16:42
  • As for me, the class with setters and getters will be better. – Netherwire May 18 '14 at 16:44