Syntactic Sugar for Virtual Constructors?

Janice Caron caron800 at googlemail.com
Thu Feb 28 09:13:20 PST 2008


On 28/02/2008, Robert Fraser <fraserofthenight at gmail.com> wrote:
> Why not just return the instance of MyClass? More flexible, since it
>  doesn't need to have a default constructor in the general case:

No, that would be /less/ flexible, since the factory function would
have to know about every possible derived class at compile time. The
whole point of a generic factory function is that it has no
compile-time knowledge of the derived classes. /All/ information about
derived classes is delayed until runtime.

The only practical way to do that is to call Object.factory(), and
since Object.factory() takes a string parameter, a string is what the
function needs to supply.



More information about the Digitalmars-d mailing list