Which C++ operator cannot be overloaded by user code? Identify the operator that is excluded from overloading rules.

Difficulty: Easy

Correct Answer: ?:

Explanation:


Introduction:
C++ permits overloading many operators to customize behavior for user-defined types. However, a fixed subset of operators is non-overloadable. Recognizing these exceptions is essential for API design.


Given Data / Assumptions:

  • Language: C++.
  • We consider built-in operator overloading rules.
  • Goal: identify a non-overloadable operator.


Concept / Approach:
Operators that cannot be overloaded include . (member access), ., ::, ?: (conditional), sizeof, typeid, alignof, and a few more context operators. In contrast, [], ->, and * are all overloadable (operator[], operator->, operator*).


Step-by-Step Solution:
1) Review overloading rules: many unary/binary operators are overloadable.2) Identify exceptions list that includes the conditional operator.3) From the options, ?: is on the non-overloadable list.4) Conclude that ?: cannot be overloaded.


Verification / Alternative check:
Attempting to declare T operator ?:(...) is ill-formed. Meanwhile, operator[], operator->, and operator* are common in smart pointers, containers, and iterator types.


Why Other Options Are Wrong:
[]: overloadable (e.g., std::vector::operator[]).->: overloadable (used by smart pointers to forward member access).*: overloadable (e.g., dereference for iterators or user types).


Common Pitfalls:
Confusing “possible but discouraged” with “impossible.” While some overloads are poor style, they are still legal; ?: is not.


Final Answer:
?:

More Questions from OOPS Concepts

Discussion & Comments

No comments yet. Be the first to comment!
Join Discussion