Objects
An object is a concrete instance of a type: it has a type, a storage location, and a lifetime. Touching it before its lifetime starts or after it ends is undefined behavior, whatever the pointer still says.
Detailed Explanation
A class is a blueprint. An object is the thing built from it, occupying real bytes at a real address. Three questions answer almost everything about an object: what type is it, where does it live, and how long does it live.
The "where" is called storage duration, and it is a property of the object, not of the class.
- Automatic — a local variable inside a function or block. It is created when control reaches the declaration and destroyed when the enclosing block exits, including via an early
returnor a thrown exception. This is the stack, and it is where most objects should live. - Static — a global, a namespace-scope variable, a
staticlocal, or a static data member. It exists for the whole program run. Namespace-scope statics are constructed beforemain, in an order that is unspecified across translation units; astaticlocal is constructed on first use and, since C++11, thread-safely. - Dynamic — created by
new(or by a container on your behalf) and living until something callsdelete. Nothing destroys it automatically, which is exactly why you wrap it instd::unique_ptrorstd::shared_ptrinstead of managing it by hand. A fourth duration,thread_local, behaves like static but with one copy per thread.
Lifetime is the sharper idea. An object's lifetime begins when its constructor completes, and it ends when its destructor starts running. The storage may exist on either side of that window, and that gap is where the bugs live. Inside a constructor body, members are alive but the object itself is not yet; calling a virtual function there dispatches to the class being constructed, not to the derived override. Inside a destructor body, the lifetime has already ended.
Using an object outside its lifetime is undefined behavior. Not "it crashes" — undefined. The compiler is allowed to assume it never happens, so it can delete the surrounding check, keep a stale value in a register, or produce code that appears to work for a year and then does not. Returning a reference to a local, reading through a pointer after delete, or holding an iterator into a vector that has since reallocated are all this same mistake.
Objects in C++ have value semantics by default. Session b = a; constructs a second, independent session. b = a; overwrites an existing session with a copy of another. Nothing is shared unless the class deliberately shares it. That is the opposite of Java, where the variable holds a reference and the copy is free because there is no copy. The C++ version costs whatever copying the members costs, which is why move operations exist for the case where the source is about to be thrown away.
Value semantics also splits equality in two. Identity asks whether two names refer to the same object, answered by comparing addresses: &a == &b. Value equality asks whether two objects hold equal state, answered by an operator== you define. Two distinct sessions can compare equal; Java's default equals conflates the two until you override it, while C++ gives you no operator== at all until you write one or, in C++20, default it.
Code Examples
#include <memory>
#include <string>
#include <vector>
struct Session {
std::string user;
int ttlSeconds = 60;
};
Session& broken() {
Session s{"ana"};
return s; // s is destroyed at this brace; the reference dangles
} // reading through it is undefined behavior
std::vector<Session> activeSessions; // static duration: lives for the whole program
void handle() {
Session local{"ravi"}; // automatic: destroyed at the closing brace
auto shared = std::make_unique<Session>(); // dynamic storage, automatic ownership
activeSessions.push_back(local); // a copy goes into the vector
local.user = "someone else"; // the stored copy is unaffected
} // local destroyed, then the unique_ptr frees its object#include <cassert>
struct Point {
int x, y;
// C++20: ask the compiler for memberwise value equality
bool operator==(const Point&) const = default;
};
int main() {
Point a{1, 2};
Point b = a; // a second object, equal in value
assert(a == b); // value equality: true
assert(&a != &b); // identity: two different objects at two addresses
}Common Gotchas
A pointer surviving is not the object surviving
after `delete p` or after the scope of a local ends, the pointer still holds the old address. Dereferencing it is undefined behavior even if the memory looks intact, because the lifetime ended when the destructor started.
Virtual calls during construction and destruction do not dispatch to the derived class
while a base constructor runs, the derived part does not exist yet, so the call resolves to the base version. Calling a pure virtual this way is undefined behavior.
Static initialization order across translation units is unspecified
one global constructor may run before another it depends on. Use a function-local `static` returning a reference instead, which is constructed on first use.
Copying is not free and not automatic in meaning
the compiler's default copy duplicates each member. That is correct for a `std::string` member and wrong for a raw owning pointer, where both objects end up freeing the same allocation.