[C++-sig] RuntimeError(Pure virtual function called) when using iterator with abstract class

Stefan Seefeld seefeld at sympatico.ca
Mon May 17 16:19:43 CEST 2010


On 05/17/2010 09:41 AM, Mr Kun Hong wrote:
> class A
> {
> public:
>
>      class Position
>      {
>      public:
>          virtual ~Position() {};
>          virtual bool exists() const = 0;
>          virtual int getId() const = 0;
>      };
>    

A::Position is thus an abstract base class.

> class PositionWrap : public A::Position, public wrapper<A::Position>
> {
> public:
>      virtual int getId() { return this->get_override("getId")(); }
>      virtual bool exists() { return this->get_override("exists")(); }
> };
>    

You derive PositionWrap from the above, but allow for the base class' 
"exists()" to be called (if it wasn't provided by a Python derived class).

I think what you may do instead is this:

   class PositionWrap : public A::Position, public wrapper<A::Position>
   {
   public:
     bool default_exists();
     virtual bool exists()
     {
       override exists = this->get_override("exists");
       if (exists) return exists();
       else return default_exists();
     }
     ...
   };

   This makes sure you have a valid fallback, so the base class' 
"exists()" is never called.

     Stefan


-- 

       ...ich hab' noch einen Koffer in Berlin...



More information about the Cplusplus-sig mailing list