So how would you implement this class without using new? Here is the class and constructor using a reference value:
class DesValue : public ObjPtr
{
public:
int datatype; // Either #Int, #Real, or #String.
int vectorFlag; // Flag indicating value contains an Array.
int optionListName; // name of the optin list item
int * intValue; // Either nil, an Int, a Real, a String, or an Array thereof.
double * doubleValue;
string * stringValue;
vector <int> * intArrayValue;
vector <double> * doubleArrayValue;
vector <string> * stringArrayValue;
unsigned char * compressedData;
unsigned long compressedDataLength;
vector <unsigned long> uncompressedStringLengths;
std::string * uncompressedString;
int isTouched; // Flag indicating if value, stringValue, or units have been modified since this DesValue was created. Set to
true by setValue, setString, setUnits, and convertUnits.
int isSetFlag; // Flag indicating whether the contents of the DesValue is defined or undefined. If isSet is false, getValue
returns nil despite the contents of value, while getString and getUnits return the empty string despite the contents of stringValue
and units.
int unitsValue; // current string value index in $UnitsList (single or top)
int unitsValue2; // current string value index in $UnitsList (bottom)
string errorMessage; // message about last conversion of string to value
string unitsArgs; // a coded string of disallowed units
public:
// constructor
DesValue ();
DesValue (const DesValue & rhs);
DesValue & operator = (const DesValue & rhs);
// destructor
virtual ~DesValue ();
virtual DesValue * clone () { return new DesValue ( * this); }
...
}
DesValue::DesValue (const DesValue & rhs)
{
datatype = rhs.datatype;
vectorFlag = rhs.vectorFlag;
optionListName = rhs.optionListName;
if (rhs.intValue)
{
intValue = new int;
* intValue = * rhs.intValue;
}
else
intValue = NULL;
if (rhs.doubleValue)
{
doubleValue = new double;
* doubleValue = * rhs.doubleValue;
}
else
doubleValue = NULL;
if (rhs.stringValue)
{
try
{
stringValue = new string;
* stringValue = * rhs.stringValue;
}
catch (std::bad_alloc &ba)
{
char msg [1024];
sprintf_s (msg, sizeof (msg),
"A memory error has occurred that could not be handled.\n"
"Please try the operation again.\n\n"
"Message: %s\n"
"Size: %d bytes", ba.what (), rhs.stringValue -> size ());
alert (msg);
}
}
else
stringValue = NULL;
if (rhs.intArrayValue)
{
intArrayValue = new vector <int>;
* intArrayValue = * rhs.intArrayValue;
}
else
intArrayValue = NULL;
if (rhs.doubleArrayValue)
{
doubleArrayValue = new vector <double>;
* doubleArrayValue = * rhs.doubleArrayValue;
}
else
doubleArrayValue = NULL;
if (rhs.stringArrayValue)
{
stringArrayValue = new vector <string>;
* stringArrayValue = * rhs.stringArrayValue;
}
else
stringArrayValue = NULL;
if (rhs.compressedData && rhs.compressedDataLength)
{
unsigned long num = rhs.uncompressedStringLengths.size ();
if (vectorFlag && num)
{
// if a vector of strings, copy the uncompressed string lengths
uncompressedStringLengths.resize (num);
for (unsigned long i = 0; i < num; i++)
uncompressedStringLengths [i] = rhs.uncompressedStringLengths [i];
}
// copy the size of the compressed data
compressedDataLength = rhs.compressedDataLength;
// allocate and copy the compressed data
compressedData = (unsigned char *) malloc (rhs.compressedDataLength);
if ( ! compressedData)
alert ("DesValue::DesValue (const DesValue &) - unable to malloc " +
asString (rhs.compressedDataLength) + " bytes");
memcpy_s (compressedData, rhs.compressedDataLength,
rhs.compressedData, rhs.compressedDataLength);
}
else
{
compressedData = NULL;
compressedDataLength = 0;
uncompressedStringLengths.resize (0);
}
if (rhs.uncompressedString)
{
uncompressedString = new std::string;
* uncompressedString = * rhs.uncompressedString;
}
else
uncompressedString = NULL;
isTouched = rhs.isTouched;
isSetFlag = rhs.isSetFlag;
unitsValue = rhs.unitsValue;
unitsValue2 = rhs.unitsValue2;
errorMessage = rhs.errorMessage;
unitsArgs = rhs.unitsArgs;
}
Lynn