What is the output of the following C program?
[gamipress_button type=”submit” label=”Run Code Online” onclick=”window.open(‘https://coderseditor.com’,’_blank’)”]
#include<iostream> using namespace std; class Test { private: static int count; public: static Test& fun(); }; int Test::count = 0; Test& Test::fun() { Test::count++; cout<<Test::count<<" "; return *this; } int main() { Test t; t.fun().fun().fun().fun(); return 0; }
Studently Answered question October 10, 2022
Output:
Compiler Error: ‘this’ is unavailable for static member functions
Static member methods in C++ do not have access to this pointer because they can also be called by class name. Similarly, static member methods in Java are not permitted to access this and super (super is for base or parent class).
The program runs without a hitch if fun() in the example programme is made non-static.
Studently Answered question October 10, 2022