Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

After reading all of the posts herein, it struck me that you want a separate instance of %parameter_config per derived class, yet shared among all objects of the derived class type.

For clarification, assuming you had three classes derived from the base, e.g., Int, String and Float. There would be exactly three instances of %parameter_config (kept somewhere -- read on), regardless of how many Int, String or Float objects you create.

Each of the %parameter_config things has in common some basic info, plus some class-specific extra stuff added by each subclass.

If this describes your problem, then perhaps I have a solution.

Create a class, say, ParamConfig.pm, which consists basically of a blessed hash (%parameter_config ) containing the basic info required by all types. Add one more hash element: parent_type.

In the constructor (new method) of this class, accept a parameter, parent_type, which is used to initialize the parent_type hash element.

Now, in your base class (from which you will derive Int, String, Float, etc.), you would have code like this:

package BaseType; ... BEGIN { my $parameter_config = undef; # accessor - turns the ParamConfig object into a closure sub parameter_config { $parameter_config = $_[0] if @_; return $parameter_config; } } sub new { my $class = shift; # the usual prolog my $type = ref $class || $class; my $self = bless {}, $type; return $self; } ...
In your derived class (say, Int), you would have something like the following code:
package Int; use base qw( BaseType ); use ParamConfig; BEGIN { # create only one of these, at compile time my $pkg_name = __PACKAGE__; my $parameter_config = parameter_config( new ParamConfig( $pkg_name +) ); @{$parameter_config}{ 'int_specific', 'params' } = ( 'vale', 'val2' +); } sub new { my $class = shift; # the usual prolog my $type = ref $class || $class; my $self = bless SUPER->new(), $type; @{$self}{ 'Int', 'Specific', 'Variables' } = (); return $self; } ... # Thereafter, access the class-specific ParamConfig using the accessor +, parameter_config().

This scheme turns instances of ParamConfig into class-level closures (i.e., singletons), share among all members of the derived classes.

Update: the use ParamConfig; belongs in the subclass, not the base class.

dmm

If you GIVE a man a fish you feed him for a day
But,
TEACH him to fish and you feed him for a lifetime

In reply to Re: OO - problem with inheritance by dmmiller2k
in thread OO - problem with inheritance by uwevoelker

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (6)
As of 2024-04-23 12:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found