Archived

This topic is now archived and is closed to further replies.

Craazer

Looping trhu structs/class elements

Recommended Posts

Craazer    122
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?

Share this post


Link to post
Share on other sites
Armand    164
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

Share this post


Link to post
Share on other sites
Craazer    122
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.

Share this post


Link to post
Share on other sites
civguy    308
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'';
}

Share this post


Link to post
Share on other sites
LilBudyWizer    491
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.

Share this post


Link to post
Share on other sites