6 Better premodern C++

 

This chapter covers

  • Input and output
  • Memory allocation
  • Variable-length parameter lists
  • Iterators

In the evolution from C to C++, the legacy of past programming practices continues to shape the software development landscape. As we transition from examining C idioms in chapter 5, in this chapter, we focus on the persistent influence of entrenched habits within early C++ programming. This chapter considers the subtle yet significant effect of historical continuity on the structure and functionality of C++ code bases.

Despite the advancements introduced by C++, developers grappled with straying from familiar C-oriented constructs and ways of thinking. This inclination toward existing methodologies extended to the selection of library functions, sometimes hindering the use of newer C++ features. Even among those who embraced the latest capabilities of C++, there were uncertainties regarding the most effective use of some of its enhanced features.

The effects of these patterns were seen in several problems that lurk within code bases. This chapter discusses these challenges, offering insights and solutions to several pitfalls and misconceptions. By detecting these problems and understanding their resolution, we can better navigate some of the complexities of C++ programming.

6.1 Mistake 36: Input and output using scanf and printf

6.2 Mistake 37: Overusing endl

6.3 Mistake 38: Dynamic allocation with malloc and free

6.4 Mistake 39: Using unions for type conversion

6.5 Mistake 40: Using varargs for variable parameter lists

6.6 Mistake 41: Incorrect class initialization order

6.7 Mistake 42: Adding nonvalue types to containers

6.8 Mistake 43: Favoring indexes over iterators