H
Heiner
#include <stdio.h>
class A
{
public:
virtual A & operator= (const A &);
virtual void test(const A &);
};
class B : public A
{
public:
virtual A & operator= (const A &);
virtual void test(const A &);
};
A & A:perator= (const A & src)
{
printf("A= called\n");
return * this;
}
void A::test(const A & src)
{
printf("A::test called\n");
}
A & B:perator= (const A & src)
{
printf("B= called\n");
return * this;
}
void B::test(const A & src)
{
printf("B::test called\n");
}
int main (int)
{
A a;
B b1, b2;
printf("b1 = a: "); b1 = a;
printf("b1 = b2: "); b1 = b2;
printf("b1.test(a): "); b1.test(a);
printf("b1.test(b2): "); b1.test(b2);
return 0;
}
I would have guessed, that b1 = b2 calls B:perator=, as b1.test(b2)
calls B::test. But what I get is:
7of9# gmake && ./test
g++ main.cpp -o test
b1 = a: B= called
b1 = b2: A= called
b1.test(a): B::test called
b1.test(b2): B::test called
7of9# g++ --version
g++ (GCC) 3.4.2 [FreeBSD] 20040728
Copyright (C) 2004 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
Is operator= not polymorphic?
Heiner
(e-mail address removed)
Remove the nospam to get my real address
class A
{
public:
virtual A & operator= (const A &);
virtual void test(const A &);
};
class B : public A
{
public:
virtual A & operator= (const A &);
virtual void test(const A &);
};
A & A:perator= (const A & src)
{
printf("A= called\n");
return * this;
}
void A::test(const A & src)
{
printf("A::test called\n");
}
A & B:perator= (const A & src)
{
printf("B= called\n");
return * this;
}
void B::test(const A & src)
{
printf("B::test called\n");
}
int main (int)
{
A a;
B b1, b2;
printf("b1 = a: "); b1 = a;
printf("b1 = b2: "); b1 = b2;
printf("b1.test(a): "); b1.test(a);
printf("b1.test(b2): "); b1.test(b2);
return 0;
}
I would have guessed, that b1 = b2 calls B:perator=, as b1.test(b2)
calls B::test. But what I get is:
7of9# gmake && ./test
g++ main.cpp -o test
b1 = a: B= called
b1 = b2: A= called
b1.test(a): B::test called
b1.test(b2): B::test called
7of9# g++ --version
g++ (GCC) 3.4.2 [FreeBSD] 20040728
Copyright (C) 2004 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
Is operator= not polymorphic?
Heiner
(e-mail address removed)
Remove the nospam to get my real address