Exception to the "Big 3"?

This is a discussion on Exception to the "Big 3"? within the C++ Programming forums, part of the General Programming Boards category; Here's a question: Take a class for which the default destructor is adequate (the constructor allocated dynamic memory, but uses ...

  1. #1
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571

    Exception to the "Big 3"?

    Here's a question:

    Take a class for which the default destructor is adequate (the constructor allocated dynamic memory, but uses a member object, like std::auto_ptr, to manage the deletion of the memory)However, this data member should not be shallow copied (that would be bad karma).

    To this end, the class tries to explicity disallow any kind copying, by implementing both a copy constructor and an assignment operator (which do nothing), but makes them private (so nobody outside the class can use them).

    Normally, the "Big 3" states that if you need any of a custom copy constructor, custom assignment operator, and custom (nontrivial) destructor, you need all of them. Does that still apply here?

    Someone said the answer is "yes", but why?

    Related question: Now the 2 operators become public, and they are implemented as deep copies (each allocates memory, deep copies, and gives it to the object to manage). Does it need a destructor now?

    (Assuming the class won't be derived from, thus doesn't need a virtual destructor.)

  2. #2
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,493
    #1) I don't think it does apply. You can get by with a copy constructor and not an assignment operator (i think).
    #2) As long you know what you're doing (and you seem to), no.

    I'm not sure on my answers...

  3. #3
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,789

    Re: Exception to the "Big 3"?

    Originally posted by Cat
    Normally, the "Big 3" states that if you need any of a custom copy constructor, custom assignment operator, and custom (nontrivial) destructor, you need all of them. Does that still apply here?
    Note that the wording is that the class "generally needs all 3 if it needs one of them"....so no hard and fast rule applies

    You need the copy constructor and the assignment operator because the compiler will produce one if you dont. These functions will assign and copy using bitwise operations - they will copy a member pointer but not the memory, and will copy auto_ptr members and therefore mess them up.....therefore if you want copy or assignment, implement both, if you want neither you should declare them private and dont implement them

    The defined destructor is probably not needed if you are just storing data in an auto_ptr as that member's destructor will sort out the resource management without your intervention. Though if there is the slightest chance of a class inheriting from yours, you of course need a virtual destructor

  4. #4
    Registered User
    Join Date
    May 2003
    Posts
    148
    Maybe this Thread on comp.lang.c++ gives you an answer.

  5. #5
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571
    Ahhhhh, that thread I think explained the reason the person I know said a destructor was needed:

    But usually we'll have a std::auto_ptr<Imp> where Imp is an incomplete (not yet defined) class, and hence we'll still have to declare the destructor and define it in the .cc file to enforce that it be non-inline.
    Essentially, the problem is the default constructor will be inlined, and thus put in client code. But in the client code which actually uses it, the auto_ptr points to an incomplete type, which means the destructor can't operate upon it (after all, it doesn't know HOW to destroy an object based only on a forward declaration).

    In thinking about it, that makes sense in the scope of the conversation that the point occured in; I believe that was exactly what he meant.
    Last edited by Cat; 06-08-2003 at 09:23 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Exception handling in a large project
    By EVOEx in forum C++ Programming
    Replies: 7
    Last Post: 01-25-2009, 06:33 AM
  2. exception handling
    By coletek in forum C++ Programming
    Replies: 2
    Last Post: 01-12-2009, 04:28 PM
  3. Handle C++ exception and structured exception together
    By George2 in forum C++ Programming
    Replies: 2
    Last Post: 01-24-2008, 08:21 PM
  4. Signal and exception handling
    By nts in forum C++ Programming
    Replies: 23
    Last Post: 11-15-2007, 01:36 PM
  5. Problem with the exception class in MINGW
    By indigo0086 in forum C++ Programming
    Replies: 6
    Last Post: 01-20-2007, 12:12 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21