chapter six

6 Better Pre-modern C++

 

This chapter covers issues related to:

  • 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 the previous chapter, we focus on the persistent influence of entrenched habits within early C++ programming. This chapter considers the subtle yet significant impact of historical continuity on the structure and functionality of C++ codebases.

Despite the advancements introduced by C++, developers grappled with familiar C-oriented constructs and ways of thinking. This inclination towards 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 impact of these patterns was seen in several problems that lurk within codebases. This chapter discusses these challenges, offering insights and solutions to several pitfalls and misconceptions. By detecting these issues 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 std::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 Non-value Types to Containers

6.8 Mistake #43 Favoring Indexes Instead of Iterators