What are the basic rules and idioms for operator overloading?

Note: The answers were given in a specific order, but since many users sort answers according to votes, rather than the time they were given, here’s an index of the answers in the order in which they make the most sense: The General Syntax of operator overloading in C++ The Three Basic Rules of Operator … Read more

What is The Rule of Three?

What does copying an object mean? What are the copy constructor and the copy assignment operator? When do I need to declare them myself? How can I prevent my objects from being copied? 8 Introduction C++ treats variables of user-defined types with value semantics. This means that objects are implicitly copied in various contexts, and … Read more

When should static_cast, dynamic_cast, const_cast and reinterpret_cast be used?

What are the proper uses of: static_cast dynamic_cast const_cast reinterpret_cast C-style cast (type)value Function-style cast type(value) How does one decide which to use in which specific cases? 9 static_cast is the first cast you should attempt to use. It does things like implicit conversions between types (such as int to float, or pointer to void*), … Read more

Why is “using namespace std;” considered bad practice?

I’ve been told by others that writing using namespace std; in code is wrong, and that I should use std::cout and std::cin directly instead. Why is using namespace std; considered a bad practice? Is it inefficient or does it risk declaring ambiguous variables (variables that share the same name as a function in std namespace)? … Read more

The Definitive C++ Book Guide and List

This question’s answers are a community effort. Edit existing answers to improve this post. It is not currently accepting new answers or interactions. This question attempts to collect the few pearls among the dozens of bad C++ books that are published every year. Unlike many other programming languages, which are often picked up on the … Read more