Advanced C++ Idioms

C++ is a multi-paradigm programming language. This means that there is no single style that is always the right one to use on C++ programs. It all depends on the task at hand. For example, it is completely fine to use C++ only as a structured language, since it has all elements of C and a few more that make structured programming even easier in C++.

The main style of use of the C++ programming language is the classic object oriented style, since it is a style that is easier to reconcile with business practices that have been developed in the previous years.

However, not every project can be done with class object oriented methods. Sometimes it is more productive to use ideas developed in other programming traditions, such as symbolic, functional, and dynamic programming. C++ can also do this, but it needs a little additional thought from the part of the program designer.

The book Advanced C++ Programming Styles and Idioms discusses several idioms that can be successfully used with the C++ programming language.

Many of these idioms are not mainstream, and some of them may not be even acceptable in your organization. Despite this, these idioms could just as well  be the tool of choice for specific problem domains.

Dynamic Features in C++
Among these idioms, are techniques that have been used for a long time in dynamic languages such as Smalltalk and Lisp, such as dynamic dispatching, symbolic computing, and related techniques.

The author, James Coplien, describes how to implement object orientation based on exemplar objects, instead of class based objects. In examplar-based programming, one uses objects as the foundation for creating objects, instead of using classes — as is the normal practice in C++. One of the keys for doing this is to use factory methods to create new objects, instead of using the new keyword, which by definition requires knowledge about the specific class of the created object.

Another strategy is to use Envelope objects, which provide some of the functionality of symbols in a language such as Lisp. Using Envelope objects, one can have automatic memory management through reference counting, and a clean separation between a value and the label that contains it.

Coplien describes how to create more dynamic objects by avoid the classic hierarchy-based approach for classes. He proposes two main solutions for this problem: in the first solution, one creates a shallow hierarchy, where each class has all possible methods that could be implemented by subclasses. In this approach, each object is always crated using the interface of the parent class, and implements only the methods that are of their interest. Subclasses rely on polymorphism to receive the correct messages, and send an error whenever they don’t respond to a particular message.

This technique has the great advantage that clients don’t need to know exactly what class is being called. Also, since the differences between objects is now based only on what methods they respond to, the system is now based on generic methods, rather than on specific classes. Any class can override any method of the top class, which makes it easy to supply dynamic behavior.

The drawback of the previous approach however is that, unless we known all methods that are of interest, the system is subject to recompilation whenever a new method is added to the parent class. To avoid this problem, one can go one step further and create a dynamic dispatch system. In such a system, there is just one method to which an operation descriptor is passed, followed by a number of parameters. In this way, easy concrete subclass can add as many operations as necessary, without any change to the parent class. While such a system may not perform as quickly as a statically defined dispatch method, it is much more flexible, and allows the system to evolve without recompilation.

Finally, the books discuss how to load code dynamically, and therefore avoid linking. The approach works on most systems, however it is machine and compiler-dependent, so it should be used with care when portability is of importance for the system.

Further Reading

Check Advanced C++ Programming Styles and Idioms at Amazon.

Similar Posts:

Similar Posts

204 Comments

  1. If a travel trailer or fifth wheel requires maintenance, you want solely drop it off at the dealership and depart the dealership in your coach car.

  2. We are trying just for a ruler who might need been described by so many various sources in later occasions as a Satavahana (whether or not he was one or not).

  3. Vibrant, neon colors have been fashionable in the trendy wedding ceremony era; however, as traits have changed, so have the colour decisions.

  4. Germany – For the recent Jovanotti tour, Lorenzo Live 2018, which has been enjoying sport arenas in the main Italian cities, outstanding British lighting designer, Paul Normandale, requisitioned 44 GLP impression X4 Bar 20 from rental firm Agorà, to gentle both A and B stages throughout the show.

  5. Usually, you can count on prices for fixing overlapped teeth to be in the ballpark of $1,800 to $6,500, and insurance coverage companies view the procedures in a different way.

  6. He begins by calling his CTA (Commodity Buying and selling Advisor) and after consultation locations an order to sell short the equivalent of $10 million of the S&P 500 index on the Chicago Mercantile Exchange “CME”.

  7. Your mortgage, for instance, is secured debt; in the event you default in your mortgage funds the bank can place a lien towards or foreclose on your house.

  8. We distill all of our findings right into a simple, easy-to-read score that acts as our own “grade” for the stock.

  9. Hey there! I know this is kind of off topic but I was wondering which blog platform are you using for this site?
    I’m getting fed up of WordPress because I’ve had issues with hackers and I’m looking at alternatives for another platform.
    I would be awesome if you could point me in the direction of a good platform.

  10. He argued that these conditions, where determination making rules resembling maximising expected utility might be applied, differ in a deep approach from “uncertain” ones, where the outcomes have been likewise random, but governed by an unknown likelihood mannequin.

  11. However, extra help is needed from federal policymakers, farmers and landowners, native and state planning authorities and the general public if we are to gradual the current developments.

  12. ???????? ?????? – ??? ?????, ??? ?????? ????? ??????
    ???-?? ????????? ??? ????. ?? ?????????? ???????? ????? ??????? ?????????, ?????????? ??? ? ????? ??? ? ?????????? ??????.

    ?????? ???? ? ????? ?????? –
    ??? ???? ?? ?????, ? ???? ?????????????? ???????????? ??????????? ??? ?????????? ?
    ?????????? ??????? ???????.

    ?????? Clubnika ????? – ??? ?????? ??????
    ??????????????? ? ???????? ??????, ?? ????????? ???? ?????? ? ????????, ?????????? ? ??????????? ? ?????????? ??????,
    ?????????? ???? ?????. ? ???????? ?????? ?? ?????? ?????? ????????????
    ?? ??????? ??????? ? ???????????? ????????? ? ?????
    ????? ?????.

    ????? ????? ?????? ?????? ? ???????? ???????
    ?? ???????? ???? ?????? ? ???????? ?
    ??????????? ???????, ??????? ??????? ???
    ????????? ??????????? ? ??? ?????????.
    ??? ??? ???? ??? ? ????????
    ??????:

    ?? ???????? ??????????? ???????? ?????? ?????? ? ?????????? ?????,
    ??????? ??????? ??? ?????? ???? ? ???????.

    ?????? ? ?????? ???????, ?????????? ? ???????? ? ?????? ? ????????
    ????????? ??????? ? ??????????.

    ?????? ????? ???????? ?????? ????????? ???? ????????? ?????? ??????, ?????
    ?? ?????? ????? ????? ???-?? ????? ?
    ??????????.

    ??????????????? ? ???????? ??????, ????????????? ??????
    ? ??????????? ??????? ?????!. https://clubnika-elitecasino.rest/

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.