Difficulty: Easy
Correct Answer: A constructor is called at the time of declaration of an object.
Explanation:
Introduction / Context:
Constructors initialize objects to a valid state. Knowing precisely when they run helps you reason about resource acquisition and initialization order, which is critical for RAII patterns in C++ (Resource Acquisition Is Initialization).
Given Data / Assumptions:
new
).
Concept / Approach:
Constructors are invoked when an object is created: for automatic objects, upon entering their scope at the definition; for static-duration objects, before main
(or on first use for function-local statics with initialization guards); for dynamic objects, immediately when new
allocates and constructs. Mere use of an already-constructed object does not call the constructor again. Defining a class type itself does not trigger constructors; only creating instances does.
Step-by-Step Solution:
Verification / Alternative check:
Instrument a constructor with logging and observe when messages print: at creation, not at arbitrary “use” points nor at type declaration time.
Why Other Options Are Wrong:
“time of use of an object”: use does not trigger construction.
“time of declaration/use of a class”: classes are types; only objects have constructors called.
Common Pitfalls:
Final Answer:
A constructor is called at the time of declaration of an object.
Discussion & Comments