C++ this pointer
Question 1 |
Which of the following is true about this pointer?
It is passed as a hidden argument to all function calls | |
It is passed as a hidden argument to all non-static function calls | |
It is passed as a hidden argument to all static functions | |
None of the above |
Discuss it
Question 1 Explanation:
The ‘this’ pointer is passed as a hidden argument to all non-static member function calls and is available as a local variable within the body of all non-static functions. ‘this’ pointer is a constant pointer that holds the memory address of the current object. ‘this’ pointer is not available in static member functions as static member functions can be called without any object (with class name).
Source: ‘this’ pointer in C++
Question 2 |
What is the use of this pointer?
When local variable’s name is same as member’s name, we can access member using this pointer. | |
To return reference to the calling object | |
Can be used for chained function calls on an object | |
All of the above |
Discuss it
Question 2 Explanation:
See following example for first use.
/* local variable is same as a member's name */ class Test { private: int x; public: void setX (int x) { // The 'this' pointer is used to retrieve the object's x // hidden by the local variable 'x' this->x = x; } void print() { cout << "x = " << x << endl; } };And following example for second and third point.
#includeusing namespace std; class Test { private: int x; int y; public: Test(int x = 0, int y = 0) { this->x = x; this->y = y; } Test &setX(int a) { x = a; return *this; } Test &setY(int b) { y = b; return *this; } void print() { cout << "x = " << x << " y = " << y << endl; } }; int main() { Test obj1(5, 5); // Chained function calls. All calls modify the same object // as the same object is returned by reference obj1.setX(10).setY(20); obj1.print(); return 0; }
Question 3 |
Predict the output of following C++ program.
#include<iostream> using namespace std; class Test { private: int x; public: Test(int x = 0) { this->x = x; } void change(Test *t) { this = t; } void print() { cout << "x = " << x << endl; } }; int main() { Test obj(5); Test *ptr = new Test (10); obj.change(ptr); obj.print(); return 0; }
x = 5 | |
x = 10 | |
Compiler Error | |
Runtime Error |
Discuss it
Question 3 Explanation:
this is a const pointer, so there is an error in line "this = t;"
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; }
Inside fun2() Inside fun1() | |
Inside fun2() | |
Inside fun1() Inside fun2() | |
Compiler Error |
Discuss it
Question 4 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.
Question 5 |
Predict the output of following C++ program?
#include<iostream> using namespace std; class Test { private: int x; public: Test() {x = 0;} void destroy() { delete this; } void print() { cout << "x = " << x; } }; int main() { Test obj; obj.destroy(); obj.print(); return 0; }
x = 0 | |
undefined behavior | |
compiler error |
Discuss it
Question 5 Explanation:
delete operator works only for objects allocated using operator new (See this post). If the object is created using new, then we can do delete this, otherwise behavior is undefined.
See “delete this” in C++ for more examples.
There are 5 questions to complete.