Today I added to a custom data storage class more needed functions. Namely copy constructor and copy assignment operator. They are needed because the class contain dynamically allocated momery and without these functions in particular situations would be made only shallow copy instead of deep copy.

These functions are very simple – copy constructor:

PointData::PointData(const PointData &)
{
    initAttributes(){
        *temperature = *pd.getTemperature();
        *partialPressure = *pd.getPartialPressure();
        *massStream = *pd.getMassStream();
        *enthalpy = *pd.getEnthalpy();
        *equilibriumVaporPressure = *pd.getEquilibriumVaporPressure();
        *humidity = *pd.getHumidity();
        *relativeHumidity = *pd.getRelativeHumidity();
    }
}

copy assignment operator:

PointData & PointData::operator=(const PointData &)
{
    if(this == &pd){
        return *this
    }

    delete temperature;
    delete partialPressure;
    delete massStream;
    delete enthalpy;
    delete equilibriumVaporPressure;
    delete humidity;
    delete relativeHumidity

    *temperature = *pd.getTemperature();
    *partialPressure = *pd.getPartialPressure();
    *massStream = *pd.getMassStream();
    *enthalpy = *pd.getEnthalpy();
    *equilibriumVaporPressure = *pd.getEquilibriumVaporPressure();
    *humidity = *pd.getHumidity();
    *relativeHumidity = *pd.getRelativeHumidity()

    return *this;
}

After defining these functions assignment (and copying) of PointData class should works without problems (if I didn’t make any mistakes :).

Leave a Reply