Re: C Compiler in C++

"Lars Duening" <>
7 Jun 2002 23:44:25 -0400

          From comp.compilers

Related articles
[6 earlier articles]
Re: C Compiler in C++ (Lex Spoon) (2002-05-13)
Re: C Compiler in C++ (2002-05-17)
Re: C Compiler in C++ (2002-05-17)
Re: C Compiler in C++ (2002-05-17)
Re: C Compiler in C++ (2002-05-17)
Re: C Compiler in C++ (Joachim Durchholz) (2002-05-23)
Re: C Compiler in C++ (Lars Duening) (2002-06-07)
| List of all articles for this month |

From: "Lars Duening" <>
Newsgroups: comp.compilers
Date: 7 Jun 2002 23:44:25 -0400
Organization: Compilers Central
References: 02-05-039 02-05-055 02-05-073 02-05-086
Keywords: C, OOP
Posted-Date: 07 Jun 2002 23:44:24 EDT

On 17 May 2002 00:23:43 -0400, journeyman <> wrote:
> On 13 May 2002 01:09:33 -0400, Lex Spoon <> wrote:

> >The party line in OO design is that whenever you need to know the
> >precise class, you should instead send a message to the object and let
> >the object do something appropriate. Because different classes will
> >respond with different methods, the message lookup itself will do the
> >check for you. In the extreme, if you want to "frob" every if
> >statement, you can even make a message like frobIfYouAreAnIf() and
> >send it to the object; in the base class it does nothing, but in PTNIf
> >it does "this.frob()".
> Sure. But you have to put a {frob}IfYouAre{Interesting}() into the
> base class for multiple values of {frob} and {Interesting} and the
> base class winds up with a lot of information about the derived
> classes. It tends to balloon.

This is now going to be borderline comp.lang.c++, but I thought to mention
that C++'s template mechanism allows to let the compiler create all the
default do-nothing methods for the programmer.

The basic conceptual idea is to provide the do-nothing default as template
method (stand-alone or base class member):

    template<typename VisitedClass> void FrobIfYouAre (VisitedClass vc) {}

and for/in the frobbable classes provide the specialization

    void FrobIfYouAre (PTNIf vc) { ... }

Of course, implementing the Visitor pattern is not as straightforward as this
simple example as the static type of the visited classes is not known at
compile time, but it still can be done. For a thorough discussion of a generic
Visitor pattern implementation see Chapter 10 in Andrei Alexandrescu: "Modern
C++ Design".
Lars Duening;
PGP Key:

Post a followup to this message

Return to the comp.compilers page.
Search the comp.compilers archives again.