C++ | this pointer | 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;
}
|
(A) x = 5
(B) x = 10
(C) Compiler Error
(D) Runtime Error
Answer: (C)
Explanation: this is a const pointer, so there is an error in line “this = t;”
Quiz of this Question
Last Updated :
28 Jun, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...