c++ object copying bitcopying vs references

V

Vulcan Fire

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
 
K

Karl Heinz Buchegger

Vulcan said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top