Menu

#529 [Perl] inheritance of enums and static methods

open
perl (97)
5
2022-02-16
2005-01-19
Josh Cherry
No

As discussed on the e-mail list, enums, static methods,
and any other members accessed with "::" rather than "-
>" are in effect not inherited in Perl. Consider the
following:

%module inheritance

%inline %{

class CBase
{
public:
enum EFoo {
eFoo0,
eFoo1
};
static int StatFun(void) {return 42;};
};

class CDerived : public CBase
{
};

%}

If this is wrapped for Perl, $inheritance::CDerived::eFoo0
is undefined, and calling inheritance::CDerived::StatFun
() doesn't work. A solution is to repeat some lines from
the Perl CBase package in the CDerived package.

Discussion

  • Josh Cherry

    Josh Cherry - 2005-01-21
    • labels: --> perl
     
  • Olly Betts

    Olly Betts - 2010-09-27
    • summary: Perl inheritance of enums and static methods --> [Perl] inheritance of enums and static methods
    • assigned_to: nobody --> talby
     
  • Olly Betts

    Olly Betts - 2022-02-16

    Still the case in git master. inheritance.i as above, runme:

    #!/bin/sh
    set -ex
    ${SWIG-../preinst-swig} -perl -c++ inheritance.i
    g++ -shared -fPIC \
      `perl -E 'use Config;say "-I".$Config{archlibexp}."/CORE ".$Config{ccflags}'` \
      inheritance_wrap.cxx -o inheritance.so
    perl -I. -wE 'use strict; use inheritance; say $inheritance::CDerived::eFoo0' || true
    perl -I. -wE 'use strict; use inheritance; inheritance::CDerived::StatFun()'
    

    gives:

    + ../preinst-swig -perl -c++ inheritance.i
    + perl -E use Config;say "-I".$Config{archlibexp}."/CORE ".$Config{ccflags}
    + g++ -shared -fPIC -I/usr/lib/x86_64-linux-gnu/perl/5.32/CORE -D_REENTRANT -D_GNU_SOURCE -DDEBIAN -fwrapv -fno-strict-aliasing -pipe -I/usr/local/include -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64 inheritance_wrap.cxx -o inheritance.so
    + perl -I. -wE use strict; use inheritance; say $inheritance::CDerived::eFoo0
    Name "inheritance::CDerived::eFoo0" used only once: possible typo at -e line 1.
    Use of uninitialized value $inheritance::CDerived::eFoo0 in say at -e line 1.
    
    + perl -I. -wE use strict; use inheritance; inheritance::CDerived::StatFun()
    Undefined subroutine &inheritance::CDerived::StatFun called at -e line 1.
    
     

Log in to post a comment.