Thread

Posted on Wed Jan 25 01:26:04 2006 by je44ery
C++ Equiv of Protected?
I like how in C++ child classes inherit parent attributes/fields/members.
class CPolygon { protected: int width, height; public: void set_values (int a, int b) { width=a; height=b;} }; class CRectangle: public CPolygon { public: int area () { return (width * height); } };
In this case, the CRectangle just starts using width & height without declaring it. How possible will this be to do in OIO? I've tried this:
$ cat test_me.pl #!/usr/bin/perl -w use strict; use warnings; use Top::Middle; my $obj = Top::Middle->new( 'info' => 'some_data_or_whatever' ); $obj->method(); exit 0; $ cat Top.pm use strict; use warnings; package Top; use Object::InsideOut; my @info :Field('Standard' => 'info'); my %init_args :InitArgs = ( 'INFO' => { 'Field' => \@info, }, ); 1; $ cat Top/Middle.pm use warnings; use strict; package Top::Middle; use Object::InsideOut 'Top'; sub method { my $self = shift; my $string = $info[$$self]; print "$string\n"; } 1;
But I get this:
# ./test_me.pl Global symbol "@info" requires explicit package name at Top/Middle.pm line 12. Compilation failed in require at ./test_me.pl line 6. BEGIN failed--compilation aborted at ./test_me.pl line 6.
If I start using Exporter, which doesn't seem right, it doesn't work either.
$ cat Top.pm use strict; use warnings; package Top; use Object::InsideOut 'Exporter'; BEGIN { our @EXPORT_OK = qw(@info); } my @info :Field('Standard' => 'info'); my %init_args :InitArgs = ( 'INFO' => { 'Field' => \@info, }, ); 1; $ cat Top/Middle.pm use warnings; use strict; package Top::Middle; use Object::InsideOut 'Top' => [ qw(@info) ]; sub method { my $self = shift; my $string = $info[$$self]; print "$string\n"; } 1;
This is the result then:
# ./test_me.pl Use of uninitialized value in concatenation (.) or string at Top/Middle.pm line 13.
Direct Responses: 1701 | Write a response
Posted on Wed Jan 25 04:15:48 2006 by jdhedden in response to 1699
Re: C++ Equiv of Protected?
One class cannot (and shouldn't be forced to) share the lexical field arrays/hashes with derived classes. You have to use the accessors.

The equivalent of 'protected' on C++ member variables is to use 'restricted' accessor methods:
package Top; { use Object::InsideOut; my @info :Field('Std' => 'info', 'Restricted' => 1); my %init_args :InitArgs = ( 'INFO' => { 'Field' => \@info, }, ); } package Top::Middle; { use Object::InsideOut 'Top'; sub print_info { my $self = shift; print($self->get_info(), "n"); } } package main; my $obj = Top::Middle->new('INFO' => 'Some info'); # print($obj->get_info, "\n"); # Can't call restricted accessor $obj->print_info();
Write a response