Casting operators

  • Thread starter Thread starter Herby
  • Start date Start date
H

Herby

Given class B C and D all inherit from class A
They all override a method of the form:
Add( A^ lhs, A^ rhs );

But only specific conversions are allowed
So if i was defining Add for class D :

D::Add( A^ lhs, A^ rhs )
{
Value = ((D)lhs)->Value + ((D)rhs)->Value;

}

Ony C can be converted to type D and D itself.
Most of the time the arguments will be of type D itself.

How can i specify C can be cast to D ?
So C will intermediately convert itself to D.
But the others cannot.

How much overhead in terms of performance is this costing when
converting a C to a D
and when converting a D to itself?

Should i use safe_cast ?
 
Can someone help me on this?

I thought maybe the only way to do it is in class A
provide a virtual cast operator for D^

Then C would override this and provide an implementation to convert
itself to D^

I tried this and it does not seem to work virtually?

How can this be done?
 
Back
Top