LearnFrenzy
Sign in Take a free test

Static and Friend Members · Find the Error · Question 2

Error: a static member function marked const Answer, explanation & discussion

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

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

main.cpp
#include <iostream>

class Version {
public:
    static int major() const { return 2; }
};

int main() {
    std::cout << Version::major() << '\n';
    return 0;
}
  • A major is a keyword
  • B A static member function cannot be const: const qualifies the object pointed to by this, and a static function has no this Correct
  • C static functions must be private
  • D Version needs a constructor
Correct Answer: Option B

Drop const: static int major() { return 2; }. It cannot modify any object anyway.

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