Pages: [1]   Go Down
Author Topic: Pure Virtual Method Problems  (Read 584 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 19
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.

Logged

Global Moderator
Dallas
Offline Offline
Shannon Member
*****
Karma: 197
Posts: 12739
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset


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.
Logged

Guildford, UK
Offline Offline
Full Member
***
Karma: 0
Posts: 217
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Iain
Logged

Dallas, Texas
Offline Offline
Sr. Member
****
Karma: 3
Posts: 267
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged


Global Moderator
Dallas
Offline Offline
Shannon Member
*****
Karma: 197
Posts: 12739
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
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:
class Interface
{
public:

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

@davez5: What are the problems?
Logged

Pages: [1]   Go Up
Jump to: