473,473 Members | 1,524 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

c++ object copying bitcopying vs references

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
2 1462
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
got it thanks!
Jul 23 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Heinz | last post by:
Hi all, How can I create a method that returns a copy of an object, not a reference to the object itself. Thanks ! Heinz
6
by: Virendra Verma | last post by:
This sounds weird, but I am looking for separate behaviors for destruction of a const and non-const object. I am trying to develop a smart/auto pointer class for writing objects to disk...
12
by: marcadonis | last post by:
Hi! Does anybody know of a way that I can keep a reference to an object that I can then reuse? I tried various approaches using navigator, but these all fail in an iframe due to premission...
6
by: solex | last post by:
Hello, I am trying to use serialization to copy objects. The object in question "Institution" inherits from a parent object "Party" both are marked as <Serializable()>. Initially I can copy an...
8
by: Raterus | last post by:
Hi, I don't even know if this can be done.. Lets say I have two objects, --- Public Class Employee End Class
35
by: Frederick Gotham | last post by:
(Before I begin, please don't suggest to me to use "std::vector" rather than actual arrays.) I understand that an object can have resources (e.g. dynamically allocated memory), and so we have to...
28
by: Stef Mientki | last post by:
hello, I'm trying to build a simple functional simulator for JAL (a Pascal-like language for PICs). My first action is to translate the JAL code into Python code. The reason for this approach is...
4
by: freefighter | last post by:
Is there such a way to ensure that at a time just one variable (pointer to ref type) is used to maintain the object. This is needed for a multi-threded application where shared data should be used...
275
by: Astley Le Jasper | last post by:
Sorry for the numpty question ... How do you find the reference name of an object? So if i have this bob = modulename.objectname() how do i find that the name is 'bob'
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.