Ross
Ross

Reputation: 14415

What exactly does CGRectMake (or other Make functions) do?

I am wish to implement a similar pattern to what Apple do with CGRectMake() and all their other ~Type~Make() functions. What is the code for CGRectMake... I understand what it does, but more specifically I would like to know what it does (in terms of stack, heap et al.)?

Is it just:

inline function CGRectMake(CGFloat x,  CGFloat y, CGFloat width, CGFloat height) {
    CGRect rect;
    rect.x = x;
    rect.y = y;
    rect.width = width;
    rect.height = height;
    return rect;
}

Or is there more to it (and to be learnt!)?

edit Is there a reason they don't make it a constructor?

Upvotes: 0

Views: 434

Answers (2)

rckoenes
rckoenes

Reputation: 69459

What you see is it, since it's a struct not a class there really ins't much more to it. Since it's a struct it is stored in the stack.

The CGRectMake function is just a convenience method for make a CGRect.


Also see the great answer by Matthias Bauch about cmd + mouse click on a method to get to it's implementation.

You can als alt+ mouse click to get the methods description.

Upvotes: 1

Matthias Bauch
Matthias Bauch

Reputation: 90117

If you command-click on CGRectMake Xcode shows you its implementation. You almost had it ;-)

CG_INLINE CGRect
CGRectMake(CGFloat x, CGFloat y, CGFloat width, CGFloat height)
{
  CGRect rect;
  rect.origin.x = x; rect.origin.y = y;
  rect.size.width = width; rect.size.height = height;
  return rect;
}

Upvotes: 2

Related Questions