0

On Design Patterns book, the author lists a group of functions he would use to develop his program:

virtual void Draw(Window*)
virtual void Bounds(Rect&)
virtual bool Intersects(const Point&)
virtual void Insert(Glyph*, int)
virtual void Remove(Glyph*)
virtual Glyph* Child(int)
virtual Glyph* Parent()

And he says a Rectangle subclass of Glyph might redefine Draw as follows:

void Rectangle::Draw (Windows* w) {
    w->DrawRect(_x0, _y0, _x1, _y1);
}

I'm curious as to why you would pass an object by pointer in some of the cases but not others. One of the functions above is passing an object by reference. What kind of design decision would force one to write functions like the ones above? i.e. Why would someone pass some objects by pointer and the others by reference?

Also, if you allow a null pointer to be passed to Rectangle::Draw function, what happens there?

niamleeson
  • 189
  • 1
  • 3
  • 10
  • I think the OP means pointers vs. references, not pointers vs. by value. – Seva Alekseyev Sep 07 '15 at 02:54
  • reference and pointer would both be equally valid in that case – Ben Voigt Sep 07 '15 at 02:54
  • Yes. I'm concerned about passing objects by pointers vs references. I've changed my wording to make it more clear. Thank you for your responses. – niamleeson Sep 07 '15 at 02:56
  • niamleeson: in C when an object parameter is optional, the standard idiom is for the parameter to have pointer type. Then the function will check if it is NULL, and NULL will indicated an omitted option. This is also not uncommon in C++ – Chris Beck Sep 07 '15 at 02:58
  • 1
    Like the OP, I find the authors' design choice rather baffling. Drawing on a null pointer makes no sense, and the authors aren't doing any null-checking either. But I suspect answering the question would require mind-reading. I'll note that apparently `Bounds` is using the `Rect&` as an out parameter, which also doesn't make much sense. I suspect it might be due to the age of the book. – T.C. Sep 07 '15 at 03:08

0 Answers0