Confusion about struct in a class



  • THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED

    On 28/11/2006 at 05:45, xxxxxxxx wrote:

    User Information:
    Cinema 4D Version:   9.5-10 
    Platform:   Windows  ;   Mac OSX  ; 
    Language(s) :     C++  ;

    ---------
    Hi,

    it's a simple and basic question, but I can't find a solution. I have the following class (don't ask, why I use my own Vector class, it's part of a physical simulation code, that I want to use as a black box) :

      
    class Vector3  
    {  
    public:  
        union {  
            struct {  
                 float x, y, z;                               
            };  
            float m[3];  
        };  
      
        Vector3() { }               // blank constructor  
        Vector3(float x, float y, float z);   // constructor with 3 values  
      
       void add(Vector3 & A);  
        ... // further methods...  
    };  
      
    

    The problem is, that I get compiler errors with CodeWarrior in the lines, where I try to access the values with Vector3 v.x:

    Error: undefined identifier 'x'...

    With VC6 everything is fine!? Any tips?

    Thank you.



  • THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED

    On 28/11/2006 at 07:00, xxxxxxxx wrote:

    Ok, I fixed it with a dirty hack. What do you mean?

      
    class Vector3  
    {  
    public:  
       float x, y, z;  
       float *m;  
      
       Vector3() {m = &x;}   // blank constructor  
       Vector3(float x, float y, float z);   // constructor with 3 values  
       void add(Vector3 & A);  
        ... // further methods...  
    };  
      
    Vector3::Vector3(float x, float y, float z)  
    {  
         this->x = x;  
         this->y = y;  
         this->z = z;  
         this->m = &this-;>x;  
    };  
      
    


  • THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED

    On 28/11/2006 at 08:18, xxxxxxxx wrote:

    Hmm, I am not sure where the exact problem is or what you actually wanna do. However, I´d use the initialisation list. And &this-;>x is also unnecessary imo as it´s simply x. Also why is m a pointer? no need imo :-)

    Vector3::Vector3(float sx, float sy, float sz) : x(sx),y(sy),z(sz),m(sx) {}



  • THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED

    On 28/11/2006 at 09:24, xxxxxxxx wrote:

    Some parts of the code access the vector members with myVector.x, myVector.y... and some parts with myVector.m[0..2]. So this is the reason for the use of the union. But if you use a union, you have to struct x, y, z in order to get aligned to the same memory address as the m[3] array. My hack is to make these different accesses possible, since the noname struct causes the compiler errors.

    I'm not very familiar with initialisation list. Does your suggestion really do the task, I just described? It doesn't seem, that m is accessable as an array.



  • THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED

    On 28/11/2006 at 09:41, xxxxxxxx wrote:

    Ah. don´t know why your compiler gives an error. Have you tried naming the struct and accessing it via myVector.structName.x ? That´s how I´d have done it.

    The initialisation list is always prefered as it sometimes can save unnecessary constructor calls when initialising within the constructor body. So I am using them a lot.



  • THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED

    On 28/11/2006 at 10:46, xxxxxxxx wrote:

    accessing it via myVector.structName.x

    That would be possible of course. But I told about treating the code module as a black box. I would have to adjust a huge of code files and there are other classes with the same problem...

    But thanks for looking and thinking.


Log in to reply