Syntactic Sugar for Virtual Constructors?

Robert Fraser fraserofthenight at gmail.com
Thu Feb 28 12:38:33 PST 2008


Janice Caron wrote:
> 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.

Why not generate the string and then call Object.factory() from within 
that virtual constructor if you need the functionality, and for a 
typical factory that knows about all the subclasses, allow the cool syntax?



More information about the Digitalmars-d mailing list