Looping trhu structs/class elements

Started by
5 comments, last by Craazer 21 years, 2 months ago
Is there any better way to do this? RECT rect; //= {0,0,0,0}; // cant relay to init {} rect.top = 0; rect.bottom = 0; rect.left = 0; rect.right = 0; So is there better way looping thoes four? Is enum solution?
Advertisement
Short answer, Not really.
The names inside a struct or a class are there for the programer.
By the time It gets executed its an address and a space allocation inside the binary.

If it''s really getting you down you might try.

memset(&rect, 0, sizeof(rect));

This will be a fraction slower then zeroing the members your self. {Functional overhead} but does mean the your code is a bit shorter.

Regards,
Armand
Armand -------------------------It is a good day to code.
quote:Original post by Armand
Short answer, Not really.
The names inside a struct or a class are there for the programer.
By the time It gets executed its an address and a space allocation inside the binary.

If it''s really getting you down you might try.

memset(&rect, 0, sizeof(rect));

This will be a fraction slower then zeroing the members your self. {Functional overhead} but does mean the your code is a bit shorter.

Regards,
Armand



Hmm that would be the solution, but actualy I need to get values.

in c++ you use a constructor to initialise data
Perhaps you want something like this?

  struct RECT {  union {    struct {      int top;      int bottom;      int left;      int right;    };    int data[4];  };};#include <iostream>int main() {  RECT rect;  for (int i=0; i<4; ++i)    rect.data[i] = 0;  rect.top = 2;  rect.right = 6;  for (int i=0; i<4; ++i)    std::cout << rect.data[i] << ''\n'';}  
Why not ...


  class MyRect : public RECT{    public:        inline MyRect(void)        {            left = right = top = bottom = 0;        };        inline MyRect(const MyRect &src)        {            left = src.left;            right = src.right;            top = src.top;            bottom = src.bottom;        };        inline MyRect(long left, long top, long right, long bottom)        {            this->left = left;            this->right = right;            this->top = top;            this->bottom = bottom;        };        inline MyRect &operator =(const MyRect &src)        {            left = src.left;            right = src.right;            top = src.top;            bottom = src.bottom;            return(this);        };};int main(void){   MyRect arect(1,2,3,4), brect(arect), crect;   crect = MyRect(4,3,2,1);   arect = brect = crect;   return(0);}  


or something there abouts.
Keys to success: Ability, ambition and opportunity.
Ooh yes, thanks CivGuy this is just what I wanted!

This topic is closed to new replies.

Advertisement