Reputation: 5438
I am using a global instance of a class like Class A; A a1; Now I am using this global variable in other functions using reference as A& a2 = a1; such that all changes done locally are reflected globally. I just wanted to check whether destructor for a2 will be called as soon its local scope ends?
Please clarify.
Upvotes: 0
Views: 85
Reputation: 1968
References are nothing but pointers and in fact many compilers convert references to pointers internally. As pointers don't have a destructor so does reference. Hence at the end of function golbal object a1 will remain as it is and it's destuctor will be called only at the end of the program.
Thanks Niraj Rathi
Upvotes: 0
Reputation: 87944
a2 is a reference so it does not have a destructor.
The destructor for a1 will only be called when the program exits. What exactly are you expecting to happen when a2's local scope ends?
Upvotes: 2