an inheritance and template class riddle...
Feb 14, 2009 at 10:32pm UTC
Hi all,
Just got an interesting piece of tangled code involving template classes. It compiles and links (have not yet tested whether it runs properly). Can it be done better? In particular, is it possible to rewrite the code as to avoid the (QuasiMonomial*) explicit type casting at line 37 of the code?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
class QuasiNumber{/*...*/ };
template <class Element>
class Monomial
{
public :
Element Coefficient;
void Substitution(/*...*/ );
/*and more members*/
};
template <class Element>
class Polynomial
{
public :
int size;
Monomial<Element>* TheMonomials;
/*and more members*/
};
class QuasiMonomial: public Monomial<QuasiNumber>
{
public :
void SpecificSubstitutionRoutines(/*...*/ );
};
class QuasiPolynomial: public Polynomial<QuasiNumber>
{
public :
void SpecificSubstitutionRoutines(/*...*/ );
};
void QuasiPolynomial::SpecificSubstitutionRoutines(/*...*/ )
{
for (int i=0;i<this ->size;i++)
{
((QuasiMonomial*)(this ->TheMonomials+i))
->SpecificSubstitutionRoutines(/*...*/ );
}
}
void QuasiMonomial::SpecificSubstitutionRoutines(/*...*/ )
{
/*...*/
Substitution(/*...*/ );
}
Using explicit type casting always leaves me with an insecure feeling inside...
Topic archived. No new replies allowed.