Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re: Multiple inheritance with multiple contructors?

by adrianh (Chancellor)
on Aug 09, 2002 at 22:07 UTC ( #189068=note: print w/replies, xml ) Need Help??


in reply to Multiple inheritance with multiple contructors?

If you really want to do this, here's one way. You just need to suck out the contents of your prototype hash and use it in the newly constructed object. For example:

package Foo; sub new { my ($proto, %args) = @_; my $class = ref($proto) || $proto; $proto = ref($proto) ? $proto : {}; $proto->{Foo} = $args{Foo}; return bless {%$proto}, $class; } package Bar; sub new { my ($proto, %args) = @_; my $class = ref($proto) || $proto; $proto = ref($proto) ? $proto : {}; $proto->{Bar} = $args{Bar}; return bless {%$proto}, $class; } package FooBar; use base qw(Foo Bar); sub new { my ($proto, %args) = @_; my $self = $proto->Foo::new(%args)->Bar::new(%args); $self->{FooBar} = $args{FooBar}; return($self); }; package main; use Test::More 'no_plan'; my $fb = FooBar->new( Foo => 1, Bar => 2, FooBar => 3); isa_ok($fb, 'FooBar'); is($fb->{Foo}, 1, "Foo set"); is($fb->{Bar}, 2, "Bar set"); is($fb->{FooBar}, 3, "FooBar set");

However, I would guess what you actually need is some sort of object delegation. Is your Subclass actually "kind of" PClass1 object and a "kind of" PClass2 object? Or does it just use them?

If the latter, then you might want to take a look at Class::Delegation, Class::Facade and the section on Aggregation in perltoot.

I think it was Bertrand Meyer who said something like "You have to be careful with multiple inheritence, otherwise you get a car-owner with four wheels". :-)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://189068]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (3)
As of 2023-01-29 19:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?