C C++

Overcoming the “Most Vexing Parse” Problem

he C++ literature uses the phrase “most vexing parse” to denote one of the dark syntactic alleys of C++. It’s vexing because programmers usually don’t even know it exists; they assume that their code creates an object initialized with a temporary, whereas the compiler interprets that same code as no

Use C++0x’s Inheriting Constructors to Reduce Boilerplate Code in Class Hierarchies

ere’s a common scenario: a base class has multiple constructors, each of which takes a different number of parameters of various types. A derived class has to declare “mirror constructors” with signatures that correspond to the base class constructors. Each mirror constructor merely forwards its arguments to the matching base

Use Class Member Initializers for Cleaner and Easier-to-Maintain Constructors

ormally, you initialize data members in a constructor’s member initialization list. When a class has multiple constructors, each constructor initializes the object’s members individually, which often leads to repeating the same initialization code in various places. The use of delegating constructors solves this problem to some extent, but there are

Use the Factory Pattern to Facilitate Dynamic Typing

ood old object-oriented programming is still alive and kicking. In many C++ frameworks and applications where inheritance and virtual functions thrive you often need to create a derived object whose precise type can only be determined at runtime. Lean how Factory can boost your code’s reliability and performance. Your application

Lambda Functions Are Ready for Prime Time

our years ago, I presented a pioneer proposal for adding lambda expressions to standards C++. The lambda proposal has since undergone significant revisions and enhancements. In their new form, C++0x lambda expressions and closures enable you to write unnamed function definitions at the place of call in a concise and

Dispose of Proprietary Threading APIs and Adopt the New C++ Threading Facilities

ntil not long ago, a standardized multithreading API for C++ was a pipe dream. Almost every operating system, compiler, and framework rolled their own threading libraries. These proprietary libraries were complex, non-portable, and didn’t support object-oriented idioms such as function objects. In the following sections, you’ll learn how to create

Uniform and Convenient Initialization Syntax

lass objects use a constructor argument list as their initializer, aggregates use braces, strings use literal text with double quotes, and containers use yet another form of initialization. These diverse initialization forms confuse users and make template code more obfuscated, not to mention the fact that C++98 doesn’t provide a

Use Explicit Conversion Functions to Avert Reckless Implicit Conversions

he compiler invokes user-defined conversion functions (which are also called conversion operators) implicitly. In most cases, this process is well-behaved and intended. However, there are cases when you certainly don’t want the compiler to invoke the conversion operator implicitly—but you can’t prevent it. Several workarounds have been devised to mitigate

Who’s the Smartest of ‘Em All? Get to Know std::unique_ptr

td::auto_ptr was a move semantics pioneer. However, at that time, C++ didn’t have the facilities for supporting move operations that were both safe and efficient. After years of distilling, C++0x brings you a superior alternative called unique_ptr. unique_ptr offers the same runtime and size efficiency of auto_ptrin addition to code

No more posts to show