By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,389 Members | 1,844 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,389 IT Pros & Developers. It's quick & easy.

c++ object copying bitcopying vs references

P: n/a
Hi
Had this doubt really irking me for some time.
For some obj of type say Obj.

void fn(Obj o){
// do somethng
}

main {
Obj ob;
fn(ob); // no COPY C'TOR d'fined

}

Doubt -> Why is that in fn() inspite of a bitcopy of the obj being
done the destructor is still called. If the object itself hasn't been
constructed & rather a bit copy's done shouldnt the destructor also
not be called when fn() exits.
Regards

Ad Varma
Jul 23 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Vulcan Fire wrote:

Hi
Had this doubt really irking me for some time.
For some obj of type say Obj.

void fn(Obj o){
// do somethng
}

main {
Obj ob;
fn(ob); // no COPY C'TOR d'fined

}

Doubt -> Why is that in fn() inspite of a bitcopy of the obj being
There is no bitcopy going on - The object is memberwise copied.
done the destructor is still called.
Sure. A new object is constructed, thus the destructor is
called when that object goes out of scope.
If the object itself hasn't been
constructed
Yes it had.
& rather a bit copy's done
A bit copy is *not* done.
The object is copied by using its copy constructor.
If the programmer didn't write a copy constructor, then
the compiler synthesizes one for it.
shouldnt the destructor also
not be called when fn() exits.


The call of a function has nothing to do with it.
An object gets created. The scope for that object ends thus
the destructor is called.

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 23 '05 #2

P: n/a
got it thanks!
Jul 23 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.