User-defined conversion operator from base class

Solution 1:

It's not a design flaw. Here's why:

Entity entity = new Body();
Body body = (Body) entity;

If you were allowed to write your own user-defined conversion here, there would be two valid conversions: an attempt to just do a normal cast (which is a reference conversion, preserving identity) and your user-defined conversion.

Which should be used? Would you really want is so that these would do different things?

// Reference conversion: preserves identity
Object entity = new Body();
Body body = (Body) entity;

// User-defined conversion: creates new instance
Entity entity = new Body();
Body body = (Body) entity;

Yuk! That way madness lies, IMO. Don't forget that the compiler decides this at compile-time, based only on the compile-time types of the expressions involved.

Personally I'd go with solution C - and possibly even make it a virtual method. That way Body could override it to just return this, if you want it to be identity preserving where possible but creating a new object where necessary.

Solution 2:

Well, when you are casting Entity to Body, you are not really casting one to another, but rather casting the IntPtr to a new entity.

Why not create an explicit conversion operator from IntPtr?

public class Entity {
    public IntPtr Pointer;

    public Entity(IntPtr pointer) {
        this.Pointer = pointer;
    }
}

public class Body : Entity {
    Body(IntPtr pointer) : base(pointer) { }

    public static explicit operator Body(IntPtr ptr) {
        return new Body(ptr);
    }

    public static void Test() {
        Entity e = new Entity(new IntPtr());
        Body body = (Body)e.Pointer;
    }
}

Solution 3:

You should use your Solution B (the constructor argument); firstly, here's why not to use the other proposed solutions:

  • Solution A is merely a wrapper for Solution B;
  • Solution C is just wrong (why should a base class know how to convert itself to any subclass?)

Also, if the Body class were to contain additional properties, what should these be initialized to when you perform your cast? It's far better to use the constructor and initialize the subclass's properties as is convention in OO languages.

Solution 4:

The reason you can't do it is because it's not safe in the general case. Consider the possibilities. If you want to be able to do this because the base and derived class are interchangeable, then you really only have one class and you should merge the two. If you want to have your cast operator for the convenience of being able to downcast base to derived, then you have to consider that not every variable typed as the base class will point to an instance of the specific derived class you are trying to cast it to. It might be that way, but you would have to check first, or risk an invalid cast exception. That's why downcasting is generally frowned upon and this is nothing more than downcasting in drag. I suggest you rethink your design.

Solution 5:

How about:

public class Entity {...}

public class Body : Entity
{
  public Body(Entity sourceEntity) { this.Pointer = sourceEntity.Pointer; }
}

so in code you don't have to write:

Body someBody = new Body(previouslyUnknownEntity.Pointer);

but you can use

Body someBody = new Body(previouslyUnknownEntity);

instead.

It's just a cosmetic change, I know, but it is pretty clear and you can change the internals easily. It's also used in a wrapper pattern that I can't remember a name of (for slightly diff. purposes).
It's also clear you are creating a new entity from a provided one so should not be confusing as an operator/conversion would be.

Note: haven't used a compiler so possibility of a typo is there.