← Back to team overview

dolfin team mailing list archive

Re: Default arguments in base classes

 



Ola Skavhaug wrote:
On Mon, Sep 7, 2009 at 9:21 AM, Ola Skavhaug<skavhaug@xxxxxxxxx> wrote:
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:

Ouch, this was another, bad solution actually :)

The proposed solution (from the link above) is roughly like this:

class Base
{
   public:
      void foo(double bar = 1.0) { foo_impl(bar); }
   protected:
      virtual void foo_impl(double bar);
};

class Sub: public Base
{
   protected:
      virtual void foo_impl(double bar);
};



This is pretty ugly and I know that I'll forget about it within a day or two. I suggest abandoning default arguments for norms if that fixes the problem. We have a default argument for str(..), but I think that PyDOLFIN has its own mechanism for this.

Note that we have a related Blueprint,

    https://blueprints.launchpad.net/dolfin/+spec/default-parameters-cpp

Garth


Sorry about that...

Ola

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

References