C++ operator overloading: which keyword is used in a function signature to overload an operator?

Difficulty: Easy

Correct Answer: operator

Explanation:


Introduction / Context:
Operator overloading lets user-defined types work naturally with syntax like +, [], and <<. In C++, the mechanism is exposed by writing special function names that include a language keyword. This question asks you to identify that keyword exactly as it appears in code such as operator+ or operator[].


Given Data / Assumptions:

  • You are familiar with member and non-member overloads.
  • We consider only standard C++ keywords.
  • Examples: T operator+(const T&, const T&); T::operator[](size_t);


Concept / Approach:

The reserved word used to declare an operator overload is operator. You place the operator token after it to form the function name, for example operator== or operator*. The keyword friend can be used to grant access to a non-member overload, but it is not the keyword that makes the function an overload. The keyword override relates to virtual functions, not operators. There is no keyword named overload in C++.


Step-by-Step Solution:

Recall the syntax: return_type operatorX(parameters); Identify the fixed token: operator. Conclude the correct answer is “operator”. Note: friend is optional access control for non-member overloads.


Verification / Alternative check:

Attempting to declare overload void overload+(...) is illegal; compilers only recognize functions whose names begin with operator followed by a recognized operator token.


Why Other Options Are Wrong:

overload: not a C++ keyword.

friend: affects access, not naming of operator overloads.

override: applies to virtual functions to indicate overriding, unrelated to operator overloading per se.


Common Pitfalls:

  • Forgetting that some operators cannot be overloaded (e.g., ?:, ., ::, sizeof).


Final Answer:

operator

Discussion & Comments

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