Operator Overloading & Type Conversions Flashcards
7 cards from real CPP practice questions. Tap to flip, then mark Knew It or Still Learning — missed cards come back until you master them.
Read the first 7 Operator Overloading & Type Conversions flashcards as text
In C++20, defining operator with a default implementation allows the compiler to automatically synthesize which operators?
Answer: All six comparison operators (, =, ==, !=)
Providing a defaulted operator (and operator==) lets the compiler synthesize all six comparison operators for the class.
What is the function call operator and how is it overloaded?
Answer: It is operator() and makes class objects callable like functions
Overloading `operator()` makes a class object a functor — it can be called using function call syntax (e.g., `obj(args)`).
What is the canonical C++ pattern for implementing operator+ in terms of operator+=?
Answer: Implement operator+= as a member, then define operator+ as a non-member returning a modified copy
The canonical pattern: implement `operator+=` as a member modifying *this, then implement `operator+` as a non-member creating a copy and applying +=.
What must operator-> return when overloaded in a smart pointer class?
Answer: A raw pointer or an object that itself has operator-> defined
operator-> must return a raw pointer (which the compiler auto-dereferences to apply ->) or an object with its own operator-> so the chain continues.
What does overloaded operator* typically return in a smart pointer or iterator class?
Answer: A reference to the pointed-to object
operator* typically returns a reference (T&) to the pointed-to object, allowing it to be used as an lvalue (e.g., `*ptr = value`).
What is an implicit conversion sequence in C++?
Answer: The sequence of standard and user-defined conversions the compiler applies automatically to match types
An implicit conversion sequence is the chain of zero or more standard and user-defined conversions that the compiler applies automatically to make an argument match a parameter type.
When the `explicit` keyword is applied to a conversion operator, what effect does it have?
Answer: The conversion requires an explicit cast rather than happening implicitly
An `explicit` conversion operator (e.g., `explicit operator bool()`) means the conversion only happens with a direct cast, preventing unintended implicit conversions.