copy constructor for classes that allocate resources..

S

Søren Johansen

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
 
J

John Harrison

Søren Johansen said:
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
 

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,764
Messages
2,569,564
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top