Go Down

Topic: Pure Virtual Method Problems (Read 661 times) previous topic - next topic

davez5

I have found a few posts here and there and on avr freaks about there being problems with pure virtual methods.  Has there been a good fix for this yet?  I just ran across the problem using Arduino 0022.


Coding Badly


On a PC, it is possible for the run-time library to trap a call to a pure virtual function and do something useful; typically report a run-time error and terminate the application.  The same is not true for a microcontroller.  How is the application terminated?  How should the fault be reported?  How much code space are you willing to give up to be able to trap the fault?

I suggest avoiding pure virtual functions.

sixeyes

Is there any other way to do "interfaces" in C++, other than using pure abstract classes?

Iain

gerg

Yes. ABCs are a special case in that they can not be directly instantiated - they can only be used as a base class. You are always free to use non-ABCs for your base classes. Simply declare the methods virtual and provide a default implementation. Override the default behavior in your derived class with whichever implementation you desire.
http://maniacalbits.blogspot.com

Coding Badly

Code: [Select]
class Interface
{
public:

 virtual unsigned AddRef( void ) = 0;
};

class Whatever : public Interface
{
public:

 virtual unsigned AddRef( void )
 {
   return( ++_refs );
 }
 
private:
 unsigned _refs;
};

void setup( void )
{
 Whatever t;
 Interface* i;
 
 i = &t;
 
 i->AddRef();
}

void loop( void )
{
}


A better choice...

Code: [Select]
class Interface
{
public:

  virtual unsigned AddRef( void )
  {
    return( 0 );
  }
};


@davez5: What are the problems?

Go Up