Hi, I have pointer object and i have to assign to another variable ( new object ). But if i change any value in new object should not reflect in old object. I did in the below way, it changes the value.
Now both object's num have value as 2;
obj.num // has 2
obj_n.num // has 2
Actually my expected result is,
obj.num - should have value 1
obj_n.num - should have value 2
My actual scenario is different where Test obj is pointer object, so obj should be pointer object. i have given sample scenario.
obj.num=1; // i guess you meant obj->num
The above does not make sense because you have declared obj to be a pointer to an object of class Test. However it is not pointing to anything. You will first have to dynamically allocate memory as follows:
Test* obj = new Test(); // creates a new object of type Test on the heap and pointer obj points to it.
Also, since you have not explicitly overridden the copy constructor, it is better to create the new object the following way.
Test obj_n; // new object of type Test created on the stack.