What is the rule of three or five?
Simple meaning
If I write a destructor, copy constructor, or copy assignment, I likely need all three.
Open the full page for Why, Steps, Example and Key takeaway.
Panel-ready C++ interview set questions for freshers and experienced developers. Practice at Coding Cadre in Faridabad, or Online from Delhi NCR.
If I write a destructor, copy constructor, or copy assignment, I likely need all three.
Open the full page for Why, Steps, Example and Key takeaway.
Resource acquisition is initialization: the constructor takes the resource, the destructor releases it.
Open the full page for Why, Steps, Example and Key takeaway.
Unique_ptr is exclusive ownership, zero overhead, move-only.
Open the full page for Why, Steps, Example and Key takeaway.
To break shared_ptr cycles, like parent-child observers.
Open the full page for Why, Steps, Example and Key takeaway.
A move constructor steals resources from an rvalue so we copy pointers, not buffers.
Open the full page for Why, Steps, Example and Key takeaway.
The compiler generates a function per type I call it with.
Open the full page for Why, Steps, Example and Key takeaway.
Two bases share a grand-base, so a derived object would get two copies of the grand-base.
Open the full page for Why, Steps, Example and Key takeaway.
A per-class table of function pointers for virtual methods.
Open the full page for Why, Steps, Example and Key takeaway.
Realloc or insert can move elements, so old iterators and pointers dangle.
Open the full page for Why, Steps, Example and Key takeaway.
An anonymous function object.
Open the full page for Why, Steps, Example and Key takeaway.
The basic guarantee leaves the program valid
Open the full page for Why, Steps, Example and Key takeaway.
Virtual enables dynamic dispatch so a base pointer can call the derived override.
Open the full page for Why, Steps, Example and Key takeaway.
unique_ptr and shared_ptr manage lifetime automatically so I rarely call delete.
Open the full page for Why, Steps, Example and Key takeaway.
I mark methods and params const when they should not modify state.
Open the full page for Why, Steps, Example and Key takeaway.
Exclusive ownership of a heap object that deletes on destruction.
Open the full page for Why, Steps, Example and Key takeaway.
Atomic refcount updates add overhead and can bounce cache lines.
Open the full page for Why, Steps, Example and Key takeaway.
It casts to an rvalue so move constructors can steal resources.
Open the full page for Why, Steps, Example and Key takeaway.
In templates it preserves value category when passing arguments onward.
Open the full page for Why, Steps, Example and Key takeaway.
map keeps keys ordered and has stable iterators references more often
Open the full page for Why, Steps, Example and Key takeaway.
Reallocation invalidates all iterators and references.
Open the full page for Why, Steps, Example and Key takeaway.