473,698 Members | 2,439 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Class Question

Sorry about the newbie question. If a class contains a pointer to some
data, and memory is allocated to that pointer in the constructor.
Naturally the destructor should free that memory, right? So since C++ is
by default call-by-value, if you pass that class as an arguement to a
function a copy of the class will be made however a copy of the data
isn't. Thus when the function reaches its end, the class destructor will
be called thus destroying the data. However the original object still
exists and when it trys to reference the data an error will result. So
finally, my question is appart from passing the class as a reference
arguement or pointer is there anything that can be done to prevent this?

Thanks,
Charles

Jul 19 '05 #1
2 2771
Charles Keepax wrote:
Sorry about the newbie question. If a class contains a pointer to some
data, and memory is allocated to that pointer in the constructor.
Naturally the destructor should free that memory, right?
Yes.
So since C++
is by default call-by-value, if you pass that class as an arguement to
a function a copy of the class will be made however a copy of the data
isn't.
If you call-by-value, the object is copied, but the data is not part of
the object. The pointer is, and so the pointer will be copied, but not
the data it points to.
Thus when the function reaches its end, the class destructor
will be called thus destroying the data. However the original object
still exists and when it trys to reference the data an error will
result.
Exactly.
So finally, my question is appart from passing the class as a
reference arguement or pointer is there anything that can be done to
prevent this?


Yes, follow the "Rule of Three": If your class needs one of a copy
constructor, assignment operator or destructor, it probably needs all
three of them.
IOW, just write your own copy constructor and assignment operator that
do the necessary allocation and copying of the data your pointer points
to. Alternatively, you could use reference counting, i.e. you count how
many objects have a pointer to the same data, and only deallocate the
memory when the last object is destroyed.
Jul 19 '05 #2
Thanks.

Charles

Rolf Magnus wrote:
Charles Keepax wrote:

Sorry about the newbie question. If a class contains a pointer to some
data, and memory is allocated to that pointer in the constructor.
Naturally the destructor should free that memory, right?

Yes.

So since C++
is by default call-by-value, if you pass that class as an arguement to
a function a copy of the class will be made however a copy of the data
isn't.

If you call-by-value, the object is copied, but the data is not part of
the object. The pointer is, and so the pointer will be copied, but not
the data it points to.

Thus when the function reaches its end, the class destructor
will be called thus destroying the data. However the original object
still exists and when it trys to reference the data an error will
result.

Exactly.

So finally, my question is appart from passing the class as a
reference arguement or pointer is there anything that can be done to
prevent this?

Yes, follow the "Rule of Three": If your class needs one of a copy
constructor, assignment operator or destructor, it probably needs all
three of them.
IOW, just write your own copy constructor and assignment operator that
do the necessary allocation and copying of the data your pointer points
to. Alternatively, you could use reference counting, i.e. you count how
many objects have a pointer to the same data, and only deallocate the
memory when the last object is destroyed.


Jul 19 '05 #3

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

Similar topics

8
2851
by: Bryan Parkoff | last post by:
I find an interesting issue that one base class has only one copy for each derived class. It looks like that one base class will be copied into three base classes while derived class from base class is executed. It means that three derived classes are pointed to a separated copy of base class. I do not allow second and third copy of base class to be created, but it must remain only first copy of base class. It allows three derived...
20
2882
by: modemer | last post by:
Question is as in subject. For example: class BaseClass { public: void func() { do something; } // I don't want this function being overloaded in its inherited class };
1
2266
by: Alfonso Morra | last post by:
if I have a class template declared as ff: (BTW is this a partial specialization? - I think it is) template <typename T1, myenum_1 e1=OK, my_enum_2=NONE> class A { public: A(); virtual ~A() ;
21
4072
by: Jon Slaughter | last post by:
I have a class that is basicaly duplicated throughout several files with only members names changing according to the class name yet with virtually the exact same coding going on. e.g. class A { std::vector<B*> Bs; public:
0
1433
by: Sebastian Hiller | last post by:
Hello, i'm new to .Net (i'm using VB as language and i'm working in the code-behind mode) and i can't solve the following problem: I have a WebForm and want to Add a UserControl (classname:QuestionControl) as many times as there are rows in a DataTable (also named Questions) in a DataSet. But this UserControl is ,for reasons of structuring, not a member of the WebForm Object in which it should be displayed, it is member of another class...
6
2113
by: rodchar | last post by:
Hey all, I'm trying to understand Master/Detail concepts in VB.NET. If I do a data adapter fill for both customer and orders from Northwind where should that dataset live? What client is responsible for instantiating the orders class? Would it be the ui layer or the master class in the business layer? thanks,
2
1603
by: Chris Puncher | last post by:
Hi. I have a RCW class that was generated by the VS.NET2003 IDE when I added a COM dll reference to my project. This all works fine when I call methods on it. My initial problem is that in ASP.NET I would like to add this to the Session object whilst using SQLServer as the session storage. Unfortunately, as this required the RCW class to be serializable, this won't work, and throws an exception saying you can't Serialize the Session...
9
1651
by: needin4mation | last post by:
I have a .aspx file and it's src. I also have a third file, a class that the src references. Everything is in the same directory/folder. Everything has the same namespace. How do I reference the class in my c# .src file? I don't believe this is an ASP.NET question, but a OOP C# question, thus my post here. I have searched and I thought it was with: using FilterSQL; //the name of my class using reports.FilterSQL; //name of...
43
2071
by: Tony | last post by:
I'm working with GUI messaging and note that MFC encapsulates the message loop inside of a C++ class member function. Is this somehow inherently less robust than calling the message loop functions within main? (It just doesn't "feel" right to me). Example 1: class MyProg { public:
2
1504
by: K Viltersten | last post by:
Suppose there is the following class structure. class S {...} class A : S {...} class B : S {...} class A1 : A {void m(){...}} class B1 : B {void m(){...}} class B2 : B {...} Just to clarify the issue. A1 and B1 share a method (void m() ) AND B2 doesn't have that
0
8683
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9170
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9031
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
7741
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4372
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4624
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3052
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 we have to send another system
2
2341
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2007
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.