ProgrammingModerateMicrosoft/Google SDE MCQ · GATE CS

Which line fails to compile?

Which line fails to compile?
struct S {
    int v = 1;
    static int count;
    static int get() { return v; }      // line 1
    static int all() { return count; }  // line 2
    int both() { return v + count; }    // line 3
};
int S::count = 0;
  1. A. Line 2
  2. B. Line 3
  3. C. None of them
  4. D. Line 1

Correct answer: D. Line 1

A static member function has no this pointer, so it cannot use the non-static member v. It can use static members (line 2), and a non-static member function can use both (line 3).

More OOP with C++ and Java questions

Exams that ask this topic