develooper Front page | perl.inline | Postings from March 2019

Abstract base classes with Inline:CPP ?

From:
Jim Avera
Date:
March 26, 2019 07:20
Subject:
Abstract base classes with Inline:CPP ?
Message ID:
41c7167f-0fa3-3a1b-84b6-efbb818a94f0@gmail.com
Hi, Is there a way to make abstract base classes play well with 
Inline::CPP ?

An xs wrapper is generated for every method in the base class, even 
though the base class, being abstract, can never be instantiated 
directly (only via a derived class which implements the virtual methods 
not defined in the base).   This causes compilation errors.

Here's an example:

#!/usr/bin/perl
use strict; use warnings; use v5.10;

package Some::Mumble::Pie;

use Inline CPP => config => namespace => 'Some';
use Inline CPP => <<'END_CPP' => CLEAN_AFTER_BUILD => 0;

#include <iostream>
using namespace std;

class Base {
   public:
     Base()          { cout << "creating a Base()" << endl; }
     virtual ~Base() { cout << "deleting a Base()" << endl; }
     void run(int a) {
       cout << "Base::run(" << a << ") called." << endl;
       tick(a);
     }
     virtual void tick(int) = 0;
};
class Der : public Base {
   public:
     Der()           { cout << "creating a Der()" << endl; }
     virtual ~Der()  { cout << "deleting a Der()" << endl; }
     void tick(int a){ cout << "Der::tick(" << a <<") called." << endl; }
};

// int main() {
//   Der *obj = new Der;
//   obj->run(42);
//   delete(obj);
//   return 0;
// }

END_CPP
1;

my $obj = Some::Der->new();
$obj->run(42);



nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About