Skip to content
Related Articles

Related Articles

C++ | this pointer | Question 4
  • Difficulty Level : Medium
  • Last Updated : 31 Jul, 2013

Predict the output of following C++ program




#include<iostream>
using namespace std;
  
class Test
{
private:
  int x;
  int y;
public:
  Test(int x = 0, int y = 0) { this->x = x; this->y = y; }
  static void fun1() { cout << "Inside fun1()"; }
  static void fun2() { cout << "Inside fun2()"; this->fun1(); }
};
  
int main()
{
  Test obj;
  obj.fun2();
  return 0;
}

(A) Inside fun2() Inside fun1()
(B) Inside fun2()
(C) Inside fun1() Inside fun2()
(D) Compiler Error


Answer: (D)

Explanation: There is error in fun2(). It is a static function and tries to access this pointer.

this pointer is not available to static member functions as static member function can be called without any object.

Quiz of this Question

Want to learn from the best curated videos and practice problems, check out the C Foundation Course for Basic to Advanced C.
My Personal Notes arrow_drop_up
Recommended Articles
Page :