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!!!
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;