On Sat, 08 Oct 2005 13:04:34 +0200, Markus Moll wrote:
What exactly do you want to do?
Thanks for both answers. I have a container class AC containing A
instances. This container can be cloned: it clones all A instances as well
(by calling A::operator=). Now there are derived classes: BC is
derived from AC and B from A. Therefore the container BC contains B
instances (and maybe A instances as well), which can be cloned. I have not
dublicated the clone code of AC, as I thought virtual operator= will do
the magic. Actually it didn't. So I changed my code to:
#include <stdio.h>
class A
{
public:
virtual A & operator= (const A &);
};
class B : public A
{
public:
virtual A & operator= (const A &);
virtual B & operator= (const B &);
};
A & A::operator= (const A & src)
{
printf("A= called, ");
return * this;
}
A & B::operator= (const A & src)
{
printf("B.1= called, ");
A::operator=(sr c);
const B * srcB = dynamic_cast<co nst B *>(& src);
if (srcB)
printf("B.2= called, ");
return * this;
}
B & B::operator= (const B & src)
{
printf("B.3= called, ");
operator=((cons t A &)src);
return * this;
}
int main (int)
{
A a;
B b1, b2;
printf("b1 = a: "); b1 = a;
printf("\nb1 = b2: "); b1 = b2;
A* a1 = & b1, *a2 = &b2;
printf("\n*a1 = *a2: "); *a1 = *a2;
printf("\n");
return 0;
}
which now gives:
7of9# gmake && ./test
g++ main.cpp -o test
b1 = a: B.1= called, A= called,
b1 = b2: B.3= called, B.1= called, A= called, B.2= called,
*a1 = *a2: B.1= called, A= called, B.2= called,
I think that is how it should be implemented.
Or is there a dynamic_cast free solution as well?
Heiner
h.********@nosp am.gmx.de
Remove the nospam to get my real address