type cast problem

I want to call a member of a parent class but call a member of the derived class - and I don't know why...

The classes look like:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class base{
...
public:
int operator-=(base&);
...
}

class child1 : public base{
...
int operator-=(base&);
int operator-=(child1&);
...
}

class child2 : public base{
...
int operator-=(child2&);
...
}


There is a function A that returns a reference to a child1 or child2.

 
base& A(int n);


I thought that the call

1
2
3
int i,j,n;
...
n = (A(i)-=A(j));


would result in a call to

 
int base::-=(base&)


but if A(i) and A(j) returned each a reference to an instance of child1 then

 
int child1::-=(base&)


is called. That's the first thing I don't understand: If both are child1&, why not

 
int child1::-=(child1&)


?
However, that's not my main problem. I modified the call to

1
2
3
int i,j,n;
...
n = (dynamic_cast<base&>(A(i))-=dynamic_cast<base&>(A(j)));


but still

 
int child1::-=(base&)


is called! Does anybody know why this happens? And more importantly: How can I fix this? I really need to call the operator of the base class.

btw if A returns references of instances of child2 everything works fine (means the operator of the base class is called). But on the other hand there is no operator in child2 that takes a value of type base&.
Lack of polymorphism is the problem here (aka - lack of virtual functions).
Sorry for the post. In the end it turned out, that I forgot to remove the virtual in the base class - so actually it was too much polymorphism...
Topic archived. No new replies allowed.