0

I have an object that contains, as part of its data, a linked list. Let's call this object LL, for reference sake.

I want to apply what I will call "operators" to instances of LL. So, an operator will, for instance, swap two elements of LL's linked list.

Ordinarily one might put such operators as methods of LL (so, you'd call LL.swap() or whatever) but I want to be able to define new types of operators.

The obvious thing to do, it seems to me, is to define an "Operator" class of objects that accept a pointer to an LL object when constructed. You could then call Operator.go() which would perform the swap.

However, this just doesn't seem right to me (for vague reasons I'm finding hard to articulate).

Other salient facts include:

  1. I will want to perform many of these operations in sequence (so perhaps the overhead should be as low as possible).
  2. There will usually only be a small number of LL objects instantiated.

Is defining an "Operator" class the way to go? Am I crazy for thinking it should be otherwise? The problem is: I'm having trouble imagining what "otherwise" might be. I haven't done any programming in a while and I'm slow in re-adjusting my brain to it.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
  • Naming a class like a C++ keyword (case apart) is IMO a very bad idea. That being apart, do you know what the STL is about, what functors are and what `std::transform` provides? – Benoit Nov 09 '11 at 11:44
  • A free function is the common approach. It can even be templated for maximum generality. The downside is that you can only use the class's public interface. – Kerrek SB Nov 09 '11 at 11:46
  • Instead of using a special method for the operator classes, just overload the `operator()` method? – Some programmer dude Nov 09 '11 at 11:46
  • @Benoit: And do _you_ know that the STL has no `std` namespace? And to which keyword are you referring? – Lightness Races in Orbit Nov 09 '11 at 11:47
  • @Tomalak Geret'kal: `operator`. Also I have read [this answer](http://stackoverflow.com/questions/5205491/whats-this-stl-vs-c-standard-library-fight-all-about/5205571#5205571) several times. – Benoit Nov 09 '11 at 11:54
  • @Benoit: Where is it suggested that the type will be called `operator`? Touché on the other bit ;) – Lightness Races in Orbit Nov 09 '11 at 12:03
  • @Tomalak Geret'kal: *“The obvious thing to do, it seems to me, is to define an "Operator" class of objects”* – Benoit Nov 09 '11 at 12:07
  • @Benoit: point taken. I would probably not name anything "Operator" when it comes to implementation. – Bob Heffernan Nov 09 '11 at 12:07
  • @Joachim: I envision having many different operators acting on the same object. – Bob Heffernan Nov 09 '11 at 12:08
  • @Benoit: `Operator` and `operator` are not the same sequence of characters. [edit: I just noticed you _did_ say "case apart". Oops.] – Lightness Races in Orbit Nov 09 '11 at 12:08
  • @Tomalak Geret'kal: That's why I said “case apart” in my first comment! – Benoit Nov 09 '11 at 12:08
  • Hmm ok, I thought you were going to one operator class per operation. – Some programmer dude Nov 09 '11 at 12:10
  • @KerrekSB: Using a "free function" seems to go against the grain when using the OO paradigm, but perhaps it is the best way to go. (Although Bjorn's suggestion of visitor-patterns below might what I want). – Bob Heffernan Nov 09 '11 at 12:12
  • @BobHeffernan: Well, lucky us then that C++ doesn't force a single paradigm down our throats :-) Have you checked out the standard library? It's full of free functions left right and centre. The best solution is often the one that reflects your needs and design most accurately and naturally, and not necessarily the one that's object oriented to the extreme. – Kerrek SB Nov 09 '11 at 12:18

1 Answers1

1

What you have thought of there is called the visitor-pattern, and it is a good and flexible way to add behavior to objects.

Björn Pollex
  • 75,346
  • 28
  • 201
  • 283
  • ... where "good" and "flexible" are very relative :-) Alexandrescu says that the visitor pattern is terrible and ugly, but that it sometimes can be the right tool -- it depends very much on the nature of the code base. In general, it's appropriate when the class hierarchy is very stable and you just want to add new behaviour a lot. – Kerrek SB Nov 09 '11 at 11:55
  • This may be exactly what I want (even taking @KerrekSB's comments into account). – Bob Heffernan Nov 09 '11 at 12:16