Search

S.7.x — Chapter 7 summary and quiz

Quick review

std::string offers an easy and safe way to deal with text strings. String literals are always placed between double quotes.

Enumerated types let us define our own type where all of the possible values are enumerated. These are great for categorizing things.

Enum classes work like enums but offer more type safety . . . → Read More: S.7.x — Chapter 7 summary and quiz

6.17 — Unnamed and inline namespaces

C++ supports two permutations on regular namespaces that are worth at least knowing about. We won’t build on these, so consider this lesson optional for now.

Unnamed (anonymous) namespaces

An unnamed namespace (also called an anonymous namespace) is a namespace that is defined without a name, like so:

This prints:

. . . → Read More: 6.17 — Unnamed and inline namespaces

6.5 — Variable shadowing (name hiding)

Each block defines its own scope region. So what happens when we have a variable inside a nested block that has the same name as a variable in an outer block? When this happens, the nested variable “hides” the outer variable in areas where they are both in scope. This is called name hiding or . . . → Read More: 6.5 — Variable shadowing (name hiding)

6.8 — Global constants and inline variables

In some applications, certain symbolic constants may need to be used throughout your code (not just in one location). These can include physics or mathematical constants that don’t change (e.g. pi or Avogadro’s number), or application-specific “tuning” values (e.g. friction or gravity coefficients). Instead of redefining these constants in every file that needs them (a . . . → Read More: 6.8 — Global constants and inline variables

6.7 — External linkage

In the prior lesson (6.6 — Internal linkage), we discussed how internal linkage limits the use of an identifier to a single file. In this lesson, we’ll explore the concept of external linkage.

An identifier with external linkage can be seen and used both from the file in which it is defined, and from other . . . → Read More: 6.7 — External linkage

6.6 — Internal linkage

In lesson 6.3 — Local variables, we said, “An identifier’s linkage determines whether other declarations of that name refer to the same object or not”, and we discussed how local variables have no linkage.

Global variable and functions identifiers can have either internal linkage or external linkage. We’ll cover the internal linkage case in this . . . → Read More: 6.6 — Internal linkage

7.16 — Lambda captures

Capture clauses and capture by value

In the previous lesson, we introduced this example:

Now, let’s modify the nut example and let the user pick a substring to search for. This isn’t as intuitive as you might expect.

This code won’t compile. Unlike nested blocks, where any identifier defined in an . . . → Read More: 7.16 — Lambda captures

7.15 — Introduction to lambdas (anonymous functions)

Consider this snippet of code that we introduced in a previous lesson:

This code searches through an array of strings looking for the first element that contains the substring “nut”. Thus, it produces the result:

Found walnut

And while it works, it could be improved.

The root of the issue here is that std::find_if . . . → Read More: 7.15 — Introduction to lambdas (anonymous functions)

6.18 — Introduction to standard library algorithms

New programmers typically spend a lot of time writing custom loops to perform relatively simple tasks, such as sorting or counting or searching arrays. These loops can be problematic, both in terms of how easy it is to make an error, and in terms of overall maintainability, as loops can be hard to understand.

Because . . . → Read More: 6.18 — Introduction to standard library algorithms

6.17 — Introduction to iterators

Iterating through an array (or other structure) of data is quite a common thing to do in programming. And so far, we’ve covered many different ways to do so: with loops and an index (for-loops and while loops), with pointers and pointer arithmetic, and with range-based for-loops:

Looping using indexes is more typing than . . . → Read More: 6.17 — Introduction to iterators