What does operator overloading allow?
You give operators like + or << a meaning for your own classes; you cannot invent operators or change their precedence.
Operator Overloading · Exercise 1 of 4
Twenty multiple-choice questions on C++ operator overloading: rules, member vs non-member operators, comparisons and the spaceship operator, stream operators, increment, conversions and functors. Pick an option to check your answer instantly.
What does operator overloading allow?
You give operators like + or << a meaning for your own classes; you cannot invent operators or change their precedence.
Which of these operators cannot be overloaded?
The conditional operator, ::, ., .*, sizeof, typeid and alignof cannot be overloaded.
Which group contains only operators that can not be overloaded?
These operate on names and types rather than values, so they are fixed by the language.
Which operators must be overloaded as non-static member functions?
The language requires these four to be members.
Why is operator<< for printing a class written as a non-member (often a friend)?
As a member, the call would have to be written obj << std::cout.
Which declaration overloads the postfix increment x++?
The unused int parameter distinguishes postfix from prefix.
What is the conventional return type of prefix operator++?
Like ++i on an int, the result is the incremented object itself (an lvalue).
Why does operator= conventionally return T& (*this)?
It mirrors the behaviour of assignment for built-in types.
After overloading + and * for a Vec class, how is a + b * c evaluated?
Precedence, associativity and arity come from the grammar, not from your functions.
Can you write int operator+(int a, int b)?
Otherwise you could change the meaning of built-in arithmetic.
Timed online tests with instant scores and explanations.