juan@rose.ACA.MCC.COM (John Reynolds) (12/19/90)
While attempting to create a fixed length and variable
length array class, I discovered the following interesting
effect of overloading the = operator using a friend function.
In order to 'copy' each class to the other, I created a friend
function for operator= for each permutation, (varray,varray),
(varray,farray), (farray,varray), and (farray,farray).
class farray
{
// some stuff
public:
void friend operator=(farray,varray);
// more stuff
}
void operator=(farray,array)
{
// some code
}
This worked fine, using the friend functions I was able to copy
with ease between the two classes.
The hidden time bomb came when I tried to use = with any other
classes. Since I had used global overloading of = in the friend
functions, the compiler would no longer default to bitwise copy
for the other classes. Apparently once you overload = for any
classes as a friend function, you must overload it explicitly
for any classes it is applied to.
I escaped this morass by making my farray and varray classes
friends of each other and using standard overloading of =.
This has the disadvantage of allowing each class access to all
of the data of the other, but it does not create a globally
overloaded = with the problems outlined above.
class farray
{
friend class varray
public:
void operator=(varray);
}
void farray::operator=(varray n)
{
// some code
}
This is a very subtle (to me) effect which I have not seen
in the literature. I understand the difference between the
two cases, but I do not understand why the compiler does not
"fall back" on bitwise copy if an explicit overload is not
supplied ( I expect a warning message, not an error ).
I hope this helps you avoid the same pit.
John Reynolds
juan@mcc.comsdm@cs.brown.edu (Scott Meyers) (12/19/90)
In article <1655@rose.ACA.MCC.COM> juan@radar.UUCP () writes: | In order to 'copy' each class to the other, I created a friend | function for operator= for each permutation, (varray,varray), | (varray,farray), (farray,varray), and (farray,farray). | | class farray | { | // some stuff | public: | void friend operator=(farray,varray); | // more stuff | } | | void operator=(farray,array) | { | // some code | } You can't write a global operator= function; it must be a member function. See ARM, p. 334. Just out of curiousity, what compiler let you get away with that? cfront 2.0 and 2.1 reject it as anachronistic, although g++ seems to think it's okay... Scott ------------------------------------------------------------------------------- Good news, Providence! Buddy Cianci is under 24-hour police protection due to threats on his life. There may yet be hope for "America's best city."