can anyone explain this code | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

can anyone explain this code

#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; } how to retify this error!!!

22nd May 2021, 6:43 AM
Sridevi R
1 Answer
+ 2
you cant use this=t; this is a pointer with a set address you cant change instead you can use x=t->x;
22nd May 2021, 6:51 AM
Mihail
Mihail - avatar