Question 4
Back to Exercise
The program below does not compile. Which statement correctly describes the problem?
#include <iostream>
class Shape {
public:
virtual static const char* kind() { return "shape"; }
virtual ~Shape() = default;
};
int main() {
std::cout << Shape::kind() << '\n';
return 0;
}
- A const char* cannot be returned
- B A member function cannot be both static and virtual: virtual dispatch needs an object, and static functions have none Correct
- C virtual must come after static
- D Shape must be abstract
Correct Answer: Option B
Use a normal virtual function if behaviour must depend on the object's type.
Discussion & Comments 0
Share your method, a shortcut, or ask about a step you didn't follow.
No comments yet. Be the first to share your solution!