← Back to team overview

dolfin team mailing list archive

Default arguments in base classes

 

We've run into problems since only GenericMatrix provides the default
argument for norm type. Swig does not like this and omits generating a
constructor for the derived types. According to
https://www.securecoding.cert.org/confluence/display/cplusplus/OBJ04-CPP.+Prefer+not+to+give+virtual+functions+default+argument+initializers
virtual function should not provide default arguments at it tends to
"defeat polymorphism":


class Thing {
  public:
    virtual ~Thing();
    virtual void doItNTimes( int numTimes = 10 );
    virtual void doItThisHigh( double howHigh = 1.0 );
    // ...
};
class MyThing : public Thing {
  public:
    void doItNTimes( int numTimes );
    void doItThisHigh( double howHigh = 2.2 );
    // ...
};

MyThing *mt = new MyThing;
Thing *t = mt;
t->doItNTimes(); // default of 10
mt->doItNTimes();  // compile time error!
t->doItThisHigh(); // default of 1.0!
mt->doItThisHigh(); // default of 2.2


Is this the intended behaviour anyhow?

This is the proposed solution:

static double const minimumHeight = 1.0;
// ...
namespace XYZ {
class Thing {
  public:
    virtual ~Thing();
    virtual void doItThisHigh( double howHigh = minimumHeight );
    // ...
};
}
// ...
namespace XYZ {
static double const minimumHeight = 2.2;
class MyThing : public Thing {
  public:
    void doItThisHigh( double howHigh = minimumHeight );
    // ...
};
}


Resolving this issue also means getting the Python interface back on track.

Ola


-- 
Ola Skavhaug


Follow ups