+ 1
I don't understand something
struct Val { int nr; }; void func(Val* v) { Val* w = v; w->nr = 10; } int main() { Val* v = new Val; v->nr = 4; func(v); cout << v->nr; //10 } Why is 10 displayed and not 4? I mean, I just changed w, not v. And "v" is not initialized "Val*& v", but it changes.
1 Antwort
+ 4
You are assingned v pointer into w in function. So now w is alias for v. Both are references. So change in one reflect back on other original..
v is initialized Val* v = new Val;