copy constructor for classes that allocate resources..

Discussion in 'C++' started by Søren Johansen, Jul 14, 2003.

  1. 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
    Søren Johansen, Jul 14, 2003
    #1
    1. Advertising

  2. "Søren Johansen" <> wrote in message
    news:3f128374$0$5146$...
    > 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
    John Harrison, Jul 14, 2003
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Aire
    Replies:
    3
    Views:
    462
    Mike Wahler
    Jan 25, 2004
  2. shsingh
    Replies:
    13
    Views:
    739
  3. ali
    Replies:
    4
    Views:
    570
    David Harmon
    Mar 5, 2007
  4. Generic Usenet Account
    Replies:
    10
    Views:
    2,217
  5. cinsk
    Replies:
    35
    Views:
    2,591
    James Kanze
    Oct 11, 2010
Loading...

Share This Page