LearnFrenzy
Sign in Take a free test

Static and Friend Members · Find the Error · Question 4

Error: a virtual static member function Answer, explanation & discussion

  • Find the error
  • Medium
  • 0 comments
  • Answer explained
  • Moderated community
Question 4 Back to Exercise

The program below does not compile. Which statement correctly describes the problem?

main.cpp
#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!

Post your comment

Comments appear after a quick review by our team.

Search LearnFrenzy

Esc

Quick links