C++ | this pointer | Question 4
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; } |
chevron_right
filter_none
(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
Recommended Posts:
- Double Pointer (Pointer to Pointer) in C
- C++ | this pointer | Question 1
- C++ | this pointer | Question 2
- C++ | this pointer | Question 3
- C++ | this pointer | Question 5
- C | Advanced Pointer | Question 8
- C | Pointer Basics | Question 13
- C | Pointer Basics | Question 6
- C | Pointer Basics | Question 16
- C | Pointer Basics | Question 17
- C | Pointer Basics | Question 4
- C | Pointer Basics | Question 3
- C | Pointer Basics | Question 2
- C | Advanced Pointer | Question 3
- C | Advanced Pointer | Question 10



