473,386 Members | 1,706 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

copy constructor for classes that allocate resources..

Hi,

I was wondering how to go by making classes that allocate resources such as
files, windows etc. Usually, I make a private assignment operator and copy
constructor. The reason is that I think it is unclear what should happen if
you copy a "window" class - should a copy of the actual window be made or
should the two classes manipulate the same window?
This does lead to a lot of non-copyable classes though and I am wondering if
there is a neat solution that I am unaware of?

Søren
Jul 19 '05 #1
1 1654

"Søren Johansen" <so***************@hotmail.com> wrote in message
news:3f**********************@dread11.news.tele.dk ...
Hi,

I was wondering how to go by making classes that allocate resources such as files, windows etc. Usually, I make a private assignment operator and copy
constructor. The reason is that I think it is unclear what should happen if you copy a "window" class - should a copy of the actual window be made or
should the two classes manipulate the same window?
By copy the actual window, do you mean a new window would popup on the user
interface? I think that is the implication but I don't think that would be
useful, but if you think different go for it.

On the other hand the two classes manipulate the same window then I think
that could be useful. Only when the last object has been destructed would
the resource be released. You would probably need to implement some sort of
reference counting scheme to make this work.

Essentially you are talking about the difference between 'value semantics'
and 'pointer semantics'. If the classes share the resource then they are
both 'pointing' at it. On the other hand if the two classes hold a seperate
resource then they both have a seperate 'value', each value could be changed
independently of the other. In the case of GUI objects I think pointer
semantics are more useful, but its just my opinion. I'd argue the same for
files, but even more strongly.
This does lead to a lot of non-copyable classes though and I am wondering if there is a neat solution that I am unaware of?

Søren


A reference counted class with pointer semantics would be my first choice.

john
Jul 19 '05 #2

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

Similar topics

39
by: scooter | last post by:
Given this class heirarchy class Base{}; class A : public Base {}; class B : public Base {};
42
by: Edward Diener | last post by:
Coming from the C++ world I can not understand the reason why copy constructors are not used in the .NET framework. A copy constructor creates an object from a copy of another object of the same...
8
by: RonHiler | last post by:
My copy constructor is crashing my program, and I can't figure out why. I'll try to make the code listing as short as I can. Here are the two headers: class BreakthroughClass { public:...
2
by: pragtideep | last post by:
Kindly help me explain the behaviour of defult copy constructor . Why the destructor is freeing the SAME memory twice , though it was allocated just once . #include<iostream> using namespace...
10
by: utab | last post by:
Dear all, So passing and returning a class object is the time when to include the definition of the copy constructor into the class definition. But if we don't call by value or return by value, ...
8
by: rKrishna | last post by:
I was trying to understand the real need for copy constructors. From literature, the main reason for redfinition of copy constructor in a program is to allow deep copying; meaning ability to make...
14
by: 2005 | last post by:
If a Constructor can be used to initialize, when is memory is allocated / say the "new" operator etc? Thanks
13
by: blangela | last post by:
I have decided (see earlier post) to paste my Word doc here so that it will be simpler for people to provide feedback (by directly inserting their comments in the post). I will post it in 3 parts...
13
by: JD | last post by:
Hi, My associate has written a copy constructor for a class. Now I need to add an operator = to the class. Is there a way to do it without change her code (copy constructor) at all? Your help...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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...

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.