c++ - Initialize an H5::CompType as a static member of a class -
i using library hdf5 save in binary.
i have sort of user defined "global" data types initialize @ beginning , use when needed.
for example want define compound type "vector" (which struct components 2 doubles: x,y).
i tried implementing idea in following way (that took answer: https://stackoverflow.com/a/27088552/4746978 )
// inside vector.h struct vector { double x; double y; } // inside hdf5types.h #include "vector.h" class hdf5types { private: static h5::comptype m_vectortype; public: static const h5::comptype& getvectortype(); }; //inside hdf5types.cpp #include "hdf5types.h" h5::comptype hdf5types::m_vectortype = hdf5types::getvectortype(); const h5::comptype& hdf5types::getvectortype() { struct initializer { initializer() { m_vectortype = h5::comptype(sizeof(vector)); m_vectortype.insertmember("x", hoffset(vector, x), h5::predtype::native_double); m_vectortype.insertmember("y", hoffset(vector, y), h5::predtype::native_double); } }; static initializer listinitializationguard; return m_vectortype; }
the code compiles problem @ runtime exception thrown:
exception thrown: read access violation.
this-> nullptr.
"this" refers object called "idcomponent" in hdf5 library. not sure how proceed since not have time dig library. perhaps knows hdf5 has solution!
you assigning value during startup of program. you're static assignment calling hdf5 library functionality, has not been instantiated yet. sigsev.
what this:
// inside hdf5types.h #include <h5cpp.h> #include "vector.h" class hdf5types{ private: static h5::comptype* m_vectortype; public: static const h5::comptype& getvectortype(); hdf5types(); }; #include "hdf5types.h" h5::comptype* hdf5types::m_vectortype = nullptr; hdf5types::hdf5types() {} const h5::comptype& hdf5types::getvectortype() { if (m_vectortype == nullptr) { struct initializer { initializer() { m_vectortype = new h5::comptype(sizeof(vector)); m_vectortype->insertmember("x", hoffset(vector, x), h5::predtype::native_double); m_vectortype->insertmember("y", hoffset(vector, y), h5::predtype::native_double); } }; static initializer listinitializationguard; } return *m_vectortype; }
this lazily initialise m_vectortype
.
Comments
Post a Comment