What is a virtual function in C++ and why is it important?
Virtual functions let code written against a base class use any derived class. Learn why they matter, how they work and what pitfalls to avoid.
Spread Your Knowledge. Questions with answers, tutorials and articles, newest first.
Virtual functions let code written against a base class use any derived class. Learn why they matter, how they work and what pitfalls to avoid.
Operator overloading lets +, ==, << and [] work on your own C++ types. Syntax, member vs non-member forms, C++20 defaults and rules.
In C++, class and struct differ only in default member and inheritance access. Learn the rules, examples and when to use each keyword.
The this pointer is the hidden argument that tells a C++ member function which object it works on. Learn when to use it, with chaining and const examples.
C++ adds classes, overloading, references, templates, exceptions and RAII to C. See the syntax differences side by side with working examples.
Inheritance shares class structure, polymorphism picks behaviour at run time. See modern C++ examples, a comparison table and common mistakes to avoid.
Understand how C++ inheritance works: derived classes, access modes, single, multiple and virtual inheritance, constructor order and when composition is better.
A class is a type definition; an object is an instance with its own storage and values. See constructors, lifetime, RAII and struct vs class.
C++20 adds std::popcount, countl_zero, countr_zero and bit_width to the bit header. Learn what each returns and how they replace compiler intrinsics.
std::rotl and std::rotr in the C++20 <bit> header rotate the bits of an unsigned integer without losing any. See how they work and where to use them.
Bit shifting in C++ uses the << and >> operators, not std::shift_left. See the rules, safe patterns and how std::bitset and the shift algorithms differ.
The C++ bit header offers std::bit_cast, std::byteswap and std::endian for safe bitwise conversions. See how to decode binary data without undefined behaviour.
std::bit_cast copies the bits of one trivially copyable value into another type of the same size, without undefined behaviour. Here is how and when to use it.
How arithmetic works in C++: operators, integer division, conversions, overflow, function objects like std::plus and the cmath and numeric helpers.
C++ bitwise operators work on individual bits for masks, flags and fast algorithms. See the idioms, flag enums, shift rules and common precedence mistakes.
Understand << and >> on integers, undefined shift counts, the bit header helpers, and how std::shift_left and std::shift_right move range elements.
C++ offers bitwise operators, std::bitset and the C++20 bit header for low-level work. Learn what each provides and how to avoid common bit manipulation bugs.
The C++ logical function objects wrap the &&, || and ! operators as callables. See how they work, when they do not short-circuit and what replaced not1.
In C++20 you default operator<=> and operator== to get all six relational operators. Learn ordering types, std::less, std::cmp_less and rel_ops.
The C++ arithmetic library covers operators, cmath, numeric helpers, numeric_limits, complex and ratio. See working examples and the common pitfalls.