CPP Operator Overloading & Type Conversions 2 — Questions and Answers
Question 1: What is the correct member function signature for overloading the less-than operator for class T?
- bool operator<(T other)
- bool operator<(const T& other) const (Correct answer)
- T operator<(const T& other)
- bool operator<(T& other)
Correct answer: bool operator<(const T& other) const
`bool operator<(const T& other) const` is correct — it returns bool, accepts a const reference to avoid copying, and is const-qualified since comparison does not modify the object.
Question 2: What is a conversion operator in C++?
- A constructor that takes one argument of another type
- A member function that converts an object to another type (Correct answer)
- A free function that casts primitive types
- A template used for static_cast
Correct answer: A member function that converts an object to another type
A conversion operator (e.g., `operator int() const`) is a member function that defines how an object is implicitly or explicitly converted to another type.
Question 3: Why is it often preferable to implement a symmetric binary operator (like +) as a non-member function?
- It runs faster as a non-member
- It allows implicit conversions on the left-hand operand as well (Correct answer)
- Non-member operators bypass access control
- It avoids the need to return by value
Correct answer: It allows implicit conversions on the left-hand operand as well
Non-member operators allow the compiler to apply implicit conversions to both operands, including the left-hand side, which member functions cannot support.
Question 4: For const-correctness, what does a class typically need to provide when overloading operator[]?
- Only a const version
- Only a non-const version
- Both const and non-const versions (Correct answer)
- A static version returning a pointer
Correct answer: Both const and non-const versions
Both a non-const version (returning T&) for modification and a const version (returning const T&) for read-only access are typically needed.
Question 5: What are user-defined literals in C++?
- Custom string types defined with typedef
- Operators that define custom suffixes for literal values (Correct answer)
- Macros that expand to compile-time constants
- Template specializations for numeric conversions
Correct answer: Operators that define custom suffixes for literal values
User-defined literals let you overload `operator""` with a custom suffix (e.g., `42_km`) to return user-defined types from literal expressions.
Question 6: What does the 'rule of three' state in C++?
- A class should have at most three constructors
- If you define a destructor, copy constructor, or copy assignment operator, you should define all three (Correct answer)
- Always overload +, -, and * together
- A function should not exceed three parameters
Correct answer: If you define a destructor, copy constructor, or copy assignment operator, you should define all three
If a class manually manages resources requiring a custom destructor, it almost certainly needs a custom copy constructor and copy assignment operator as well.
Question 7: In C++20, what type does the three-way comparison operator (operator<=>) return?
- bool
- int
- A comparison category such as std::strong_ordering (Correct answer)
- char
Correct answer: A comparison category such as std::strong_ordering
operator<=> returns a comparison category type (std::strong_ordering, std::weak_ordering, or std::partial_ordering) that encodes less-than, equal, or greater-than.
What is the correct member function signature for overloading the less-than operator for class T?