|
-
February 14th, 2007, 11:51 PM
#1
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!
Can you help me with my homework assignment?, Before you post!, Use code tags, How to post!, Codeguru technical FAQs, C++ FAQ Lite, Stroustrup: C++ Style and Technique FAQ, Guru of the Week, Comeau C and C++ FAQs, Comeau C++ Templates FAQs, CUJ @ DDJ, Spam threshold
My Blogs : Learning C++ is fun | Abnegator's reflections
Open Threads : C++ Aha! Moments | Nature of work in C++?
-
February 15th, 2007, 06:42 AM
#2
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
-
February 15th, 2007, 06:57 AM
#3
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()!
-
February 15th, 2007, 07:10 AM
#4
Re: Violating abstractions
i always call operator= in the constructor.
-
February 15th, 2007, 07:12 AM
#5
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.
-
February 15th, 2007, 07:16 AM
#6
Re: Violating abstractions
yes you are right for classes that are based on aray/vectors will lose alot of speed.
-
February 15th, 2007, 08:08 AM
#7
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 ?
-
February 15th, 2007, 08:54 AM
#8
Re: Violating abstractions
 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 ?
-
February 15th, 2007, 08:56 AM
#9
Re: Violating abstractions
 Originally Posted by SuperKoko
If the copy constructor throws an exception, behavior is undefined.
Yes, that too.
-
February 15th, 2007, 09:43 AM
#10
Re: Violating abstractions
 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()!
-
February 15th, 2007, 10:33 AM
#11
Re: Violating abstractions
Indeed.
-
February 15th, 2007, 11:55 AM
#12
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.
 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.
 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.
Can you help me with my homework assignment?, Before you post!, Use code tags, How to post!, Codeguru technical FAQs, C++ FAQ Lite, Stroustrup: C++ Style and Technique FAQ, Guru of the Week, Comeau C and C++ FAQs, Comeau C++ Templates FAQs, CUJ @ DDJ, Spam threshold
My Blogs : Learning C++ is fun | Abnegator's reflections
Open Threads : C++ Aha! Moments | Nature of work in C++?
-
February 15th, 2007, 12:00 PM
#13
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?
-
February 15th, 2007, 12:05 PM
#14
Re: Violating abstractions
 Originally Posted by exterminator
check for self assignment.. 
and what makes u think that is neccesary.
-
February 15th, 2007, 01:12 PM
#15
Re: Violating abstractions
 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
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|