This archive contains answers to questions sent to Unidata support through mid-2025. Note that the archive is no longer being updated. We provide the archive for reference; many of the answers presented here remain technically correct, even if somewhat outdated. For the most up-to-date information on the use of NSF Unidata software and data services, please consult the Software Documentation first.
Hi Fei Liu, > ... this is a C++ related question about netCDF class design. I am > wondering if it's possible to change NcFile implementation in the future > to declare some of its member functions virtual, especially add_dim and > add_var because they are not flexible enough. For example, I included a > ONetCDFFile class implementation inherited from NcFile where I override > add_dim function. But since add_dim is non-virtual in base class NcFile, > I have to either stick to ONetCDFFile everywhere a variable is referred to > or use dynamic_cast to downcast type. It's not possible to use polymorphic > features that C++ provided. e.g. > > ONetCDFFile of(...); > do_file(&of); > > void do_file(NcFile * of){ > of->add_dim(..); // Wrong, we really intended to use ONetCDFFile::add_dim > .. > } > > I understand the memory overhead associated with virtual functions but I > think it's a non-issue in scientific computing where large dataset is the > norm. I think it'd be a better design to declare some member functions > virtual in netcdfcpp, specifically, NcFile::add_dim, NcFile::add_var, > NcFile::~NcFile (as a general rule, always declare destructor virtual when > virtual member function is present). These are good suggestions, and we will incorporate them into the C++ interface for a future release. Unfortunately, it's too late to get your changes in the 3.6.1 release, which has been out for a week but won't be announced until next Monday. Thanks again for your efforts to improve netCDF. If you have other suggestions for improving the C++ interface, feel free to send them to address@hidden. --Russ _____________________________________________________________________ Russ Rew UCAR Unidata Program address@hidden http://www.unidata.ucar.edu