operators in an derived class do not behave as I would like

I am surprised by the behaviour of my code:

I have an instance A of some container class (containings templates). I defined an operator

 
T& operator[](int n);


in the instance A the template T is a class CVar. CVar contains amongs others the members:

1
2
void set(string s);
int operator=(CVar& M);


I derived a class

1
2
3
4
5
6
class MyINT : public CVar{
...
void set(string s);
int operator=(MyINT& M);
...
}


Somewhere in my code I call

 
A[i].set(s);


If A[i] returns a CVar, CVar::set is called.
If A[i] returns a MyINT, MyINT::set is called.

somewhere else I call

 
A[i]=A[j];


If A[i] and A[j] return CVar variables, CVar::operator= is called.
If A[i] and A[j] both return MyINT variables, still CVar::operator= is called (and not MyINT::operator= as I would desire).

Now I understand, that MyINT::set overrides CVar::set but MyINT::operator= does not override CVar::operator= because the parameters are of different type.
Well, both operators could be adressed by the same call A[i]=A[j], but for some reason CVar wins against MyINT.

Is there some way to influence, which of these operators is called?

It works if I use typeid and cast, but that's not a good solution because I've a lot of operators and childs of CVar.

Any ideas?
Last edited on
Maybe I use the information of typeid not smart enough...
Can I do something like

 
dynamic_cast<typeid(A[i])>A[i]


to force A[i] to be not interepreted as its base class?
Topic archived. No new replies allowed.