Difficulty: Easy
Correct Answer: Only once
Explanation:
Introduction / Context:
Constructors in C++ are special member functions that initialize an object into a valid state. Understanding exactly how many times a constructor is called per object helps clarify object lifetime, copy/move semantics, and where initialization truly happens versus later mutations.
Given Data / Assumptions:
Concept / Approach:
A constructor (of some form) is called exactly once for each object: at its creation. After the object exists, later assignments or method calls do not “reconstruct” it; they simply modify state. If an object is created via copy or move, the corresponding copy/move constructor is the one invoked, still exactly once for that object. Reassignment later invokes assignment operators, not constructors.
Step-by-Step Solution:
Verification / Alternative check:
Add logging to all constructors and operators. You will observe exactly one constructor log per created object, matched by at most one destructor log at end-of-life (if not elided). Copy elision can remove extra temporary constructions, but each ultimately created object is still constructed once.
Why Other Options Are Wrong:
Twice/Thrice: there is no scenario where a single object is “constructed” multiple times.
Depends on the way of creation: the specific constructor overload may differ, but the count per object remains one.
Common Pitfalls:
Final Answer:
Only once
Discussion & Comments