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

Struct passing

Started by Solstice Aug 11, 2002 at 11:26 PM 1 replies 450+ views
Original Post
Solstice
Solstice
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..."
-Solsticedeninet.comaeris.deninet.com"...I was given three choices, the earth, the stars, or..."
LockePick
LockePick
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
Oluseyi
Oluseyi
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).

Topic Locked

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

Sign in to reply to this topic.