|
By Alex, on December 21st, 2020 In lesson 3.1 — Syntax and semantic errors, we covered syntax errors, which occur when you write code that is not valid according to the grammar of the C++ language. The compiler will notify of you these types of errors, so they are trivial to catch, and usually straightforward to fix.
We also covered semantic . . . → Read More: 7.14 — Common semantic errors in C++
By Alex, on December 21st, 2020 In the previous lesson 7.12 — Introduction to testing your code, we discussed how to write and preserve simple tests. In this lesson, we’ll talk about what kind of tests are useful to write to ensure your code is correct.
Code coverage
The term code coverage is used to describe how . . . → Read More: 7.13 — Code coverage
By Alex, on December 21st, 2020 The last category of flow control statement we’ll cover is the halt. A halt is a flow control statement that terminates the program. In C++, halts are implemented as functions (rather than keywords), so our halt statements will be function calls.
Let’s take a brief detour, and recap what happens when a program exits normally. . . . → Read More: 7.11 — Halts (exiting your program early)
By Alex, on December 21st, 2020 This lesson continues our exploration of switch statements that we started in the prior lesson 7.4 — Switch statement basics. In the prior lesson, we mentioned that each set of statements underneath a label should end in a break statement or a return statement.
In this lesson, we’ll explore why, and talk about some switch . . . → Read More: 7.5 — Switch fallthrough and scoping
By Alex, on December 21st, 2020 This lesson is a continuation of lesson 7.2 — If statements and blocks. In this lesson, we’ll take a look at some common problems that occur when using if statements.
Nested if statements and the dangling else problem
It is possible to nest if statements within other if statements:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
|
#include <iostream> int main() { std::cout << "Enter a number: "; int x{}; std::cin >> x; if (x >= 10) // outer if statement // it is bad coding style to nest if statements this way if (x <= 20) // inner if statement std::cout << x << "is between 10 and 20\n"; // which if statement does this else belong to? else std::cout << x << "is greater than 20\n"; return 0; } |
The . . . → Read More: 7.3 — Common if statement problems
By Alex, on August 16th, 2020 (h/t to reader Koe for providing the first draft of this lesson!)
In lesson 20.9 — Exception specifications and noexcept, we covered the noexcept exception specifier and operator, which this lesson builds on.
We also covered the strong exception guarantee, which guarantees that if a function is interrupted by an exception, no memory will be . . . → Read More: M.5 — std::move_if_noexcept
By Alex, on August 11th, 2020 (h/t to reader Koe for providing the first draft of this lesson!)
In C++, all functions are classified as either non-throwing (do not throw exceptions) or potentially throwing (may throw an exception).
Consider the following function declaration:
|
int doSomething(); // can this function throw an exception or not? |
Looking at a typical function declaration, it is not possible to determine whether a function might throw . . . → Read More: 20.9 — Exception specifications and noexcept
By nascardriver, on January 30th, 2020 Depending on where you’re at in your journey with learning programming languages (and specifically, C++), LearnCpp.com might be the only resource you’re using to learn C++ or to look something up. LearnCpp.com is designed to explain concepts in a beginner-friendly fashion, but it simply can’t cover every aspect of the language. As you begin to . . . → Read More: 8.1 — Using a language reference
By Alex, on January 3rd, 2020 Quick review
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 and don’t pollute the encapsulating namespace quite as much.
And finally, structs offer us a way to group related variables . . . → Read More: 8.x — Chapter 8 summary and quiz
By Alex, on January 3rd, 2020 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:
|
#include <iostream> namespace // unnamed namespace { void doSomething() // can only be accessed in this file { std::cout << "v1\n"; } } int main() { doSomething(); // we can call doSomething() without a namespace prefix return 0; } |
This prints:
. . . → Read More: 6.17 — Unnamed and inline namespaces
|
|