I am trying to derive a class such that I can reuse methods & operators from the base class. E.g., in the base class, I can write
< xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />
Base b1 = new Base();
Base b3 = 3.0 * b1;
because I’ve written the ‘Scale’ method and ‘operator *’. (see below)
When I try to do this in a class derived from Base, I fail utterly. The compiler hints that “an explicit cast exists; am I forgetting it ”, but I have no idea where or how to use it!
I would prefer this kind of code
Derived d1 = new Derived();
Derived d3 = 3.0 * d1;
but the requirement to insert a cast would ruin the aesthetics.
Below is what I’ve tried so far. Any help appreciated.
= = = =
namespace InheritanceAndCasting {
class Program {
static void Main( string[] args ) {
Base b1 = new Base();
Base b3 = 3.0 * b1; // provided by Base.Scale && operator *
Console.WriteLine( b3.BaseProperty ); // 9
Derived d1 = new Derived();
Derived d3 = 3.0 * d1; // <<<<< doesnt compile, doesnt work
Console.WriteLine( d3.DerivedProperty); // 15 never gets here
}
}
//==================================================
class Base {
double bfield;
//==================================================
public Base() {
Console.WriteLine("Base()");
bfield = 3.0;
}
//==================================================
public Base(Base b) {
Console.WriteLine( "Base(Base)" );
bfield = b.bfield;
}
//==================================================
public double BaseProperty {
get {
return bfield;
}
}
//==================================================
public static Base Scale( double x, Base b ) {
Base result = new Base(b);
result.bfield *= x;
return result;
}
//==================================================
public static Base operator *( double x, Base b ) {
return Base.Scale( x, b );
}
}
//==================================================
class Derived : Base {
//==================================================
public Derived() {
Console.WriteLine("Derived()");
}
//==================================================
public Derived( Derived d )
: base( d ) {
Console.WriteLine("Derived(Derived)");
}
////==================================================
//public static new Base Scale( double y, Base d ) {
// return Base.Scale( y, d );
//}
//==================================================
public double DerivedProperty {
get {
return this.BaseProperty;
}
}
}
}

static method inheritance
TiKu
imzi
class Derived : Base {
...
public static Derived operator* ( double x, Derived b ) {
return new Derived(Base.Scale(x, b));
}
There is a problem with your implementation however. Your base implementation creates new Base objects for all its calculations but Derived can't use the newly created Base object in order to initialize itself. As required by the language at least one of the operator parameters must be of the same type as the defining class and the return value will be of the defining class. Therefore you can't simply use Base here. You therefore need to expose a way to set the value of Derived through Base. This is commonly done through a protected or private constructor as in:
private Derived ( Base b ) : base(b) { ... }
If I were implementing this code I would probably lean toward creating an instance method called Scale that scales the current object's value. I would then use it instead of the static method. The operator code would then degenerate to:
public static Derived operator* ( double x, Derived d )
{ return new Derived(d).Scale(x); }
You could still implement the static version if you needed it but given your sample I wouldn't think that you would.
Michael Taylor - 9/1/05