The first copies that value of test
into ptr
.
The second sets ptr to be a pointer to the address of test
These two actions are not the same. In the first case, ptr
will have the same value as test
, but they are in them selves two distinct copies of that data, so that your assignment of ptr(0,0) = 95;
will not set test(0, 0)
.
In the second instance however, ptr
points to the address of test
, so that the dereference of ptr
is test. Thus, when you set the value here, you are actually setting the value of test(0, 0)
as well.
This is trivially verifyable with a program such as this:
#include <iostream>
class Test{
public:
int i;
};
int main(){
Test c;
c.i = 1;
Test d = c;
d.i = 2;
std::cout << "Value: " << c.i << "\n";
Test* e = &c;
(*e).i = 2;
std::cout << "Pointer: " << c.i << "\n";
}
Of course, if you dynamically allocate your Matrix (new) then when you copy the value as per your first example, the pointer to the data is also copied, so when you set the new data, it appears to be equal to the second example.