Skip to main content
GameDev.net gamedev.net
🔒 Locked

Looping trhu structs/class elements

Started by Craazer Jan 29, 2003 at 5:09 PM 5 replies 200+ views
Original Post
Craazer
Craazer
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?
Armand
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(▭, 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.
Craazer
Craazer
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(▭, 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.

Qw3r7yU10p!
Qw3r7yU10p!
in c++ you use a constructor to initialise data
civguy
civguy
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'';
}
LilBudyWizer
LilBudyWizer
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.
Craazer
Craazer
Ooh yes, thanks CivGuy this is just what I wanted!

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.