in reply to perl inheritance

If I derive from this class and I call one of these methods, I'm interested in getting back the derived class (not the base).

The best way to do this is to fix the superclass to return new objects of the correct class. How does it return new objects?


Improve your skills with Modern Perl: the free book.

Replies are listed 'Best First'.
Re^2: perl inheritance
by Anonymous Monk on Mar 23, 2012 at 21:01 UTC

    I'm not sure exactly how I should do that. Would you have the constructor take a base class object and convert it to the derived? Something like the following:

    sub new { if (ref $class eq 'base') { //return derived object built from base class object } //return derived object built from parameters }
    Would invoking this form of the derived class constructor through AUTOLOAD be considered bad practice? This is so that all base class methods do not have to be overridden?

      Would you have the constructor take a base class object and convert it to the derived?

      I'd get the class of the provided object, then call its constructor directly.

      Something like the following...

      No. You're overcomplicating this and confusing yourself. Please post the existing code that constructs a new object so we can all see what you're doing.

        My sincere apologies for replying to you so late, Chromatic. Unfortunately, I don't have decent code worth posting at this point. Would you possibly have any example classes that you can point me to that would demonstrate what you are referring to..

        many thanks as usual. Michael