in reply to Base class constructor that blesses into subclass

I use the "Factory method" in C++, and used it a few times last week.

It seems that you're planning to directly bless the instance into different child classes.

It would be clean, and quite acceptible, to call the proper constructor for each derived class instead. That is, the factory function figures out which derived class to use, then calls 'new' on it. The factory method never directly creates or blesses anything.

Replies are listed 'Best First'.
Re: Re: Base class constructor that blesses into subclass
by mp (Deacon) on Jul 08, 2002 at 20:44 UTC
    Thank you. I did as you and Basilides suggested. The factory method determines the class name, then calls the subclass constructor like this:
    return $class_name->new(%args)
    This is cleaner than blessing into the subclass in the "parent". I also more or less incorporated Jenda's suggestion and made the subclasses all support the same methods (except for some accessors that differ across subclasses).