diff --git a/c++/src/H5DataSet.cpp b/c++/src/H5DataSet.cpp index 3a1497580f5..492c429f113 100644 --- a/c++/src/H5DataSet.cpp +++ b/c++/src/H5DataSet.cpp @@ -76,6 +76,27 @@ DataSet::DataSet(const DataSet &original) : H5Object(), AbstractDs(), id(origina incRefCount(); // increment number of references to this id } +//-------------------------------------------------------------------------- +// Function: DataSet assignment operator +///\brief Assignment operator: same HDF5 object as \a original +///\param original - IN: DataSet instance to copy +// Programmer Lee Newberg - 2021 +//-------------------------------------------------------------------------- +DataSet & +DataSet::operator=(const DataSet &original) +{ + // Wrap the changes to `*this` by an incremented reference count for + // `original` to prevent trouble in the case that `*this` and + // `original` share reference counts or memory in any relevant + // sense. + original.incRefCount(); + decRefCount(); // for old value of id + id = original.id; + incRefCount(); // for new value of id + original.decRefCount(); + return *this; +} + //-------------------------------------------------------------------------- // Function: DataSet overload constructor - dereference ///\brief Given a reference, ref, to an hdf5 location, creates a diff --git a/c++/src/H5DataSet.h b/c++/src/H5DataSet.h index c7454709cfd..333ed7a999f 100644 --- a/c++/src/H5DataSet.h +++ b/c++/src/H5DataSet.h @@ -117,6 +117,9 @@ class H5_DLLCPP DataSet : public H5Object, public AbstractDs { // Copy constructor - same as the original DataSet. DataSet(const DataSet &original); + // Assignment operator + DataSet &operator=(const DataSet &original); + // Creates a copy of an existing DataSet using its id. DataSet(const hid_t existing_id);