In C++, a constant member function can be overloaded with:
IBM aptitude question, verified with a worked answer. Free to practise - no sign-up.
In C++, a constant member function can be overloaded with:
Show answer & explanation
Answer: A. A non-const member function having the same parameter list
In C++, const and non-const member functions are considered different overloads—the const-ness is part of the function signature. A const member function can be overloaded by a non-const member function with identical parameter lists; the compiler selects which version to call based on whether the object is const or non-const. Static functions and global functions do not participate in instance-based overloading with member functions.
Step-by-step Derivation:
C++ function overloading rules:
- Const-qualification of member functions is part of the function signature.
- A const member function and non-const member function with the same name and parameter list form a valid overload set.
- Example:
class Example {
public:
void display() const { std::cout << "const version"; }
void display() { std::cout << "non-const version"; }
};
const Example obj1;
obj1.display(); // Calls const version
Example obj2;
obj2.display(); // Calls non-const version
- Static and global functions are not part of instance member function overloading. Option B and D are incorrect because they do not participate in const/non-const overloading.