Reputation: 53
I am inheriting two classes,TFoamIntegrand, and IBaseFunctionMultiDim, which are both abstract classes. The problem I have is that I want to be able to use the Clone Member function which for IBaseFunctionMultiDim is a pure virtual with no arguments, and for TFoamIntegrand it is not a pure virtual in TFoamIntegrand but has an argument (with a default value).
class TObject {
public:
virtual TObject *Clone(const char *newname="") const;
...
};
class TFoamIntegrand : public TObject {
public:
virtual Double_t Density(Int_t ndim, Double_t *) = 0;
...
};
class IBaseFunctionMultiDim {
public:
/**
Clone a function.
Each derived class must implement his version of the Clone method
*/
virtual IBaseFunctionMultiDim * Clone() const = 0;
...
}
The only way I can make it compile is to do:
class InSANEDiffXSec : public TFoamIntegrand, public ROOT::Math::IBaseFunctionMultiDim {
public:
virtual ROOT::Math::IBaseFunctionMultiDim* Clone() const ;
...
}
Of course this produces the warnings about hiding the Clone with the argument. I have tried the following but I get covariant return type errors.
virtual ROOT::Math::IBaseFunctionMultiDim* Clone(const char * newname = "") const ;
virtual InSANEDiffXSec* Clone(const char * newname = "") const ;
I thought that the last version above was the correct one, according to my understanding of covariant return types. What am I missing here?
Thanks in advance!
EDIT: Here is a complete example:
class A {
public:
virtual A* Clone(const char * name="") { return( new A());}
};
class B {
public:
virtual B* Clone() = 0;
};
class C : public A {
public:
virtual double Density(int ndim, double *) = 0;
};
class D : public C, public B {
public:
virtual double Density(int ndim, double *x) {
return 0.0;
}
//virtual D* Clone(const char * name="") { return( new D());}
virtual B* Clone() { return( new D());}
};
It compiles. Changing to the commented line gives:
test.h: In member function ‘virtual D* D::Clone(const char*)’:
test.h:24:62: error: cannot allocate an object of abstract type ‘D’
virtual D* Clone(const char * name="") { return( new D());}
^
test.h:19:7: note: because the following virtual functions are pure within ‘D’:
class D : public C, public B {
^
test.h:11:17: note: virtual B* B::Clone()
virtual B* Clone() = 0;
^
test.cxx: In function ‘int main()’:
test.cxx:7:18: error: cannot allocate an object of abstract type ‘D’
D * d = new D();
^
In file included from test.cxx:2:0:
test.h:19:7: note: since type ‘D’ has pure virtual functions
class D : public C, public B {
Upvotes: 1
Views: 416
Reputation: 275260
This compiles:
class TObject {
public:
virtual TObject *Clone(const char *newname="") const = 0;
};
class IBaseFunctionMultiDim {
public:
virtual IBaseFunctionMultiDim * Clone() const = 0;
};
class InSANEDiffXSec :
public TObject,
public IBaseFunctionMultiDim
{
public:
virtual InSANEDiffXSec* Clone(const char* newname) const
{
return nullptr;
}
virtual InSANEDiffXSec* Clone() const
{
return Clone(nullptr);
}
};
but I did remove stuff from your code that did not seem to be relevant.
Upvotes: 1