How can i see the difference between address and value c++ class? -


i have problem in pointers , reference understanding. have following snippet:

class u { protected:     int m; public:     u(int m = 0) : m(m) {}     virtual void f() { cout << "u::f() @ m = " << m << endl; } };  class v : public u {     int n; public:     v(int m, int n) : u(m), n(n) {}     void f() { cout << "v::f() @ m = " << m << ", n = " << n << endl; } };  int main() {     v v1(1, 1), v2(2, 2);     v1.f();     v2.f();     u* u = &v1;     *u = v2;     v1.f();     v2.f(); } 

i run , output is:

v::f() @ m = 1, n = 1 v::f() @ m = 2, n = 2 v::f() @ m = 2, n = 1 v::f() @ m = 2, n = 2 

i don't understand third line of output: v::f() @ m = 2, n = 1. why m changed 2?

*u=v2; 

this doesn't make u point v2. assigns v2 u sub-object of object u points to, i.e. u part of v1. because *u u object.


Comments

Popular posts from this blog

javascript - Google App Script ContentService downloadAsFile not working -

javascript - Function overwritting -

php - Find a regex to take part of Email -