C++ | this pointer | Question 4

Predict the output of following C++ program

filter_none

edit
close

play_arrow

link
brightness_4
code

#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;
}

chevron_right


(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



My Personal Notes arrow_drop_up


Article Tags :
Practice Tags :


1


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.