What Is a Smart Pointer in C++? unique_ptr, shared_ptr, weak_ptr
Smart pointers free memory automatically through RAII. Learn when to use unique_ptr, shared_ptr and weak_ptr, with runnable C++ examples.
Spread Your Knowledge. Questions with answers, tutorials and articles, newest first.
Smart pointers free memory automatically through RAII. Learn when to use unique_ptr, shared_ptr and weak_ptr, with runnable C++ examples.
A conversion constructor is a single-argument constructor that enables implicit conversion to a class type. Learn when to allow it and when to use explicit.
A copy constructor builds a new C++ object from an existing one. See its syntax, when it runs, how to write a deep copy and the rule of five.
A default constructor is callable with no arguments. See when the compiler generates one, how = default and = delete work, and why members may stay uninitialised.
Function templates let you write one generic function that works with many types. See syntax, type deduction, specialization and C++20 concepts.
A class template is a C++ blueprint the compiler uses to generate classes for any type. See the syntax, deduction, concepts and specialization.
A current comparison of C and C++ covering paradigms, memory management, standard libraries, compatibility and when to choose each language.
The new operator allocates and constructs an object, while malloc only reserves raw bytes. Compare error handling, release rules and modern alternatives.
The this pointer is the hidden argument that points to the current object in non-static member functions. See its type, common uses and pitfalls.
A pure virtual function is declared with = 0 and makes a class abstract. See syntax, a full shape example, bodies for pure virtuals and common mistakes.
A virtual function lets a base-class pointer call the derived class's override at run time. See override, final, abstract classes and common pitfalls.
A shallow copy shares pointed-to data, a deep copy duplicates it. See the default copy behaviour, copy and swap and why smart pointers prevent the bug.
A friend function is a non-member function that a class grants access to its private and protected members, used for operators and tightly coupled helpers.
An instance in C++ is an object created from a class. Learn how objects and instances differ, how they are created and how copies behave.
Namespaces group C++ names under a scope to avoid collisions. See syntax, using rules, nested and inline namespaces and best practices.
C++ reads and writes files with the ifstream, ofstream and fstream stream classes. See open modes, binary I/O, error handling and C++17 std::filesystem.
Learn the C++ stream classes (istream, ostream, fstream, stringstream), the predefined cin, cout, cerr and clog, and how to handle stream errors.
C++ allocates heap memory at run time with new and delete. Learn how it works, what goes wrong, and why smart pointers and containers are the modern default.
Constructors set up C++ objects and destructors release their resources automatically. Learn the kinds, ordering rules, virtual destructors and the rule of zero.
The C++ STL is built on templates and consists of containers, iterators, algorithms and function objects. Learn how to pick containers and use ranges.