NcFile and its non-virtual member functions

Hi Group, 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).

class ONetCDFFile : public NcFile {

public:

  ONetCDFFile(const char * pathname, NcFile::FileMode fmode = 
NcFile::ReadOnly,
              size_t * chunksizeptr = NULL, size_t initialise = 0) :
    NcFile(pathname, fmode, chunksizeptr, initialise) {
  }
  virtual ~ONetCDFFile(){ outputDims.clear(); }

  virtual NcDim * add_dim(NcToken dimname, long dimsize){
    if(outputDims.find(dimname) != outputDims.end()){
#ifdef DEBUG
      cerr << __FILE__ << ":" << __LINE__ << " duplicated call to add_dim 
" << dimname << ":" <<
        dimsize << ":" << get_dim(dimname)->size() << endl;
#endif
      return get_dim(dimname);
    }
    NcFile::add_dim(dimname, dimsize);
    outputDims.insert(map<string, NcDim *>::value_type(dimname, 
get_dim(dimname)) );
  }

  virtual NcDim * add_dim(NcToken dimname){
    if(outputDims.find(dimname) != outputDims.end()){
#ifdef DEBUG
      cerr << __FILE__ << ":" << __LINE__ << " duplicated call to add_dim 
" << dimname << endl;
#endif
      return get_dim(dimname);
    }
    NcFile::add_dim(dimname);
    outputDims.insert(map<string, NcDim *>::value_type(dimname, 
get_dim(dimname)) );
  }


private:
  map<string, NcDim *> outputDims;
};

-- 
Fei Liu, PhD.                    phone: 609-4526551
RSIS/GFDL/NOAA                   fax:   609-9875063
P.O. Box 308, 201 Forrestal Rd.  http://www.gfdl.noaa.gov/~fil
Princeton, NJ 08542-0308         email: Fei.Liu@xxxxxxxx


  • 2006 messages navigation, sorted by:
    1. Thread
    2. Subject
    3. Author
    4. Date
    5. ↑ Table Of Contents
  • Search the netcdfgroup archives: