destructor of base class in interp_1d.cpp


sean88
01-15-2015, 04:04 AM
In interp_1d.cpp, the base class do have a virtual function

Doub virtual rawinterp(Int jlo, Doub x) = 0;

so why there's no virtual destructor defined explicitly in the base class?

sean88
03-03-2015, 05:28 AM
no body here?...

Saul Teukolsky
03-03-2015, 12:31 PM
Hi Sean,

The answer to your question is that the book can't be all things to all users. It is simply not a code library for code re-use applications. It is a textbook on algorithms that, for definiteness, instantiates them in a randomly chosen language, namely C++.

In the case of the warning about virtual destructors, there are a number of NR routines that generate this warning. But putting virtual destructors on any class that later becomes a derived class would add little when the pedagogical intent is not an "is-a" relationship. That is the case in the interps. There is no useful sense in which "Poly_interp is a Base_interp". Thus an array of pointers to Poly_interp would always be declared as such, not as pointers to Base_interps, which is what the warning is trying to prevent. This is explained in detail on page 23, where "is-a" and "prerequisite" relationships are contrasted.

The compiler, of course, doesn't understand all this and issues the warning. If you like, you can make the warning go away by inserting the line

virtual ~Base_interp(void) {}

in the Base_interp class. But it's really not necessary in this case.

Best,
Saul Teukolsky

sean88
03-11-2015, 03:08 AM
Thanks very much for the clarification, Saul !