Archived

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

Solstice

Struct passing

Recommended Posts

I''m a little fuzzy on how to do something like this:
class Hotspot
{
  public:
  Hotspot(SDL_Rect r);
  SDL_Rect bounds(void);

  private:
  SDL_Rect _rect;
};

Hotspot::Hotspot(SDL_Rect r)
{
  _rect.x = r.x;
  _rect.y = r.y;
  _rect.w = r.w;
  _rect.h = r.h;
}

SDL_Rect Hotspot::bounds(void)
{
  return _rect;
}
 
Now if SDL_Rect were a class, complete with copy constructors, I would expect this to work. But SDL_Rect is a simple struct. So how do I deal with this? -Solstice deninet.com aeris.deninet.com "...I was given three choices, the earth, the stars, or..."

Share this post


Link to post
Share on other sites
Don''t quite understand why you think that won''t work. All you are doing is accessing values and copying them to another struct.
In C++, a struct is essentially the same as a class. That should work just fine.

------------
Where''d the engine go? Where''d it go? Aaaaaah!!
MSN: nmaster42@hotmail.com, AIM: LockePick42, ICQ: 74128155

Share this post


Link to post
Share on other sites
Constant references might suffice for your needs. Your constructor should take a constant reference anyway, but if the return value of bounds is only meant to be viewed/copied and not altered, then you can do the following:

Hotspot::Hotspot(const SDL_Rect & r)
{
// whatever
}
 
const SDL_Rect & Hotspot::bounds(void)
{
return rect;
}

This is efficient (eliminates unnecessary copying) and still fast (reference is 4 bytes big).

Share this post


Link to post
Share on other sites