CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 22
  1. #1
    Join Date
    Feb 2005
    Location
    "The Capital"
    Posts
    5,306

    Violating abstractions

    I saw a piece of code on one of Andrew Koenig's articles (it is not his code by the way! he criticizes it and rightly so). It was an implementation of the assignment operator. Here it goes:
    Code:
    class A {
       public:
          A& operator=(const A& t) {
             this->~A();
             new(this) A(t);
             return *this;
          }
          // other members
    };
    I just wanted to post it here and know what people wanted to say about it. Go for it!

  2. #2
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125

    Re: Violating abstractions

    If one of my devlopers ever wrote this type of code [without pages of documentation justifing it, that was approved by the review commitee), he/she would no longer be developing software for a living.
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

  3. #3
    Join Date
    Feb 2005
    Location
    Normandy in France
    Posts
    4,590

    Re: Violating abstractions

    Main problem : Exception safety.
    If the copy constructor throws an exception, behavior is undefined.
    Thus, in that case, this class doesn't even met the basic exception guarantee: namely, consistency.
    "inherit to be reused by code that uses the base class, not to reuse base class code", Sutter and Alexandrescu, C++ Coding Standards.
    Club of lovers of the C++ typecasts cute syntax: Only recorded member.

    Out of memory happens! Handle it properly!
    Say no to g_new()!

  4. #4
    Join Date
    Aug 2005
    Location
    Netherlands, The
    Posts
    2,184

    Re: Violating abstractions

    i always call operator= in the constructor.

  5. #5
    Join Date
    Sep 2001
    Location
    Bilbao (spain)
    Posts
    110

    Re: Violating abstractions

    In terms of efficiency, it doesn't recycle memory if internally uses dynamic memor, of course, if the concrete implementation uses it, and must construct/destruct all internal objects instead of reset their states. This may imply an important overhead (depending on concrete implementation).

    In terms of clarity i think it's better to implemtent an initialize/destroy pair.

  6. #6
    Join Date
    Aug 2005
    Location
    Netherlands, The
    Posts
    2,184

    Re: Violating abstractions

    yes you are right for classes that are based on aray/vectors will lose alot of speed.

  7. #7
    Join Date
    Dec 2004
    Location
    Poland
    Posts
    1,165

    Re: Violating abstractions

    This is also discussed by Sutter in one of his GoTW items, cant remember which one.
    B+!
    'There is no cat' - A. Einstein

    Use [code] [/code] tags!

    Did YOU share your photo with us at CG Members photo gallery ?

  8. #8
    Join Date
    Apr 2004
    Location
    England, Europe
    Posts
    2,492

    Re: Violating abstractions

    Quote Originally Posted by exterminator
    I saw a piece of code on one of Andrew Koenig's articles (it is not his code by the way! he criticizes it and rightly so). It was an implementation of the assignment operator. Here it goes:
    Code:
    class A {
       public:
          A& operator=(const A& t) {
             this->~A();
             new(this) A(t);
             return *this;
          }
          // other members
    };
    I just wanted to post it here and know what people wanted to say about it. Go for it!
    Without looking at any of the replies ...

    What happens if A has a virtual destructor, and/or the operator= was called from a derived class' operator= function ?
    My hobby projects:
    www.rclsoftware.org.uk

  9. #9
    Join Date
    Apr 2004
    Location
    England, Europe
    Posts
    2,492

    Re: Violating abstractions

    Quote Originally Posted by SuperKoko
    If the copy constructor throws an exception, behavior is undefined.
    Yes, that too.
    My hobby projects:
    www.rclsoftware.org.uk

  10. #10
    Join Date
    Feb 2005
    Location
    Normandy in France
    Posts
    4,590

    Re: Violating abstractions

    Quote Originally Posted by Zaccheus
    Without looking at any of the replies ...

    What happens if A has a virtual destructor, and/or the operator= was called from a derived class' operator= function ?
    The derived class object is destroyed, and a base class object is created at this place... Then, accessing/invoking any member of the derived class becomes UB.
    "inherit to be reused by code that uses the base class, not to reuse base class code", Sutter and Alexandrescu, C++ Coding Standards.
    Club of lovers of the C++ typecasts cute syntax: Only recorded member.

    Out of memory happens! Handle it properly!
    Say no to g_new()!

  11. #11
    Join Date
    Apr 2004
    Location
    England, Europe
    Posts
    2,492

    Re: Violating abstractions

    Indeed.
    My hobby projects:
    www.rclsoftware.org.uk

  12. #12
    Join Date
    Feb 2005
    Location
    "The Capital"
    Posts
    5,306

    Re: Violating abstractions

    Good responses! You have found the most complex ones (exception safety and the difference in static and dynamic type of the object of derived upon assignment if this one is its base)!

    But the easiest one that you missed was - check for self assignment..

    I think, virtual destructor alone will not cause problems, this class needs to be derived for the problem to show up, isn't it? Anyways, virtual destructor is enough hint about possibility of future extensions.
    Quote Originally Posted by TheCPUWizard
    If one of my devlopers ever wrote this type of code [without pages of documentation justifing it, that was approved by the review commitee), he/she would no longer be developing software for a living.
    Quote Originally Posted by Carlos Martinez
    In terms of efficiency, it doesn't recycle memory if internally uses dynamic memor, of course, if the concrete implementation uses it, and must construct/destruct all internal objects instead of reset their states. This may imply an important overhead (depending on concrete implementation).

    In terms of clarity i think it's better to implemtent an initialize/destroy pair.
    I am not sure, but are any of your points in support of that code?
    Last edited by exterminator; February 15th, 2007 at 12:01 PM.

  13. #13
    Join Date
    Dec 2003
    Location
    Middletown, DE
    Posts
    67

    Re: Violating abstractions

    Why is the virtual destructor called at all? An assignment operator is supposed to assign values, not destroy them.

    Flaws aside, what was the author of that snippet trying to accomplish exactly?

  14. #14
    Join Date
    Aug 2005
    Location
    Netherlands, The
    Posts
    2,184

    Re: Violating abstractions

    Quote Originally Posted by exterminator
    check for self assignment..
    and what makes u think that is neccesary.

  15. #15
    Join Date
    Apr 1999
    Location
    Altrincham, England
    Posts
    4,470

    Re: Violating abstractions

    Quote Originally Posted by Mitsukai
    and what makes u think that is neccesary.
    Because it's assigning to itself. The first thing it does is to destroy itself, then it tries to recreate itself based on... the object that was just destroyed.
    Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
    --
    Sutter and Alexandrescu, C++ Coding Standards

    Programs must be written for people to read, and only incidentally for machines to execute.

    --
    Harold Abelson and Gerald Jay Sussman

    The cheapest, fastest and most reliable components of a computer system are those that aren't there.
    -- Gordon Bell


Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured