Skip to content

Comment on Grim C++ Tales from the Crypt: The Visitor Pattern (2017)parent

Comments

I don't know about the limitations of C++, but something like what you describe would happen in Smalltalk. Take for example a Canvas object and a bunch of different shape objects (square, circle, etc):

  "Square class"
  drawOn: aCanvas
      aCanvas line: (some point) to: (some point).
      "etc"
  
  "Triangle class"
  drawOn: aCanvas
      aCanvas line: self leftVertex to: self rightVertex.
      "etc"

  "Canvas class"
  drawShapes: aCollection
      aCollection do: [ :shape |
          shape drawOn: self ]
AboutSource Built by g1lg1l

Hackerly is an independent reader for Hacker News, built on the public HN API. Not affiliated with Y Combinator.