Jump to content
  • Advertisement

Archived

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

Craazer

Looping trhu structs/class elements

This topic is 5649 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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
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

Share this post


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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!