TITLE: what is a surrogate? (Newsgroups: comp.lang.c++.moderated, 9 Sep 96) PROBLEM: nfegan@apcc.com (Noel Fegan) What is a surrogate class? RESPONSE: kanze@gabi-soft.fr (J. Kanze) The same thing as a proxy or a helper class. (Proxy seems to be the most widespread name for them. Surrogate is supposedly more precise, but proxy appeared first, and I can never remember how to spell surrogate anyway.) For a detailed description, see "More Effective C++", by Scott Meyers (item30) and "Design Patterns", by Gamma, Helm, Johnson and Vlissides. (Meyers goes into the whys and wherefores of the most frequent use in some detail, the Gang of Four have a more general and much more succinct presentation.) As a quick example, just to give you an idea, suppose you have a class with an array interface, but with the actual data on disk. You cannot have operator[] return a simple reference to the local buffer, since this would cause problems if you used it several times on the same object in the same expression. The solution is to provide a get and put function, which handle the actual referencing immediately, and have operator[] return a proxy. The proxy object simply contains a pointer to the real object, and the index. It has a conversion operator to the contained type (which simply calls get), and an assignment operator which in fact calls the put function in the container. Thus, something like "a[ i ] = a[ j ]" ends up calling "a.put( a.get( j ) , i )".