Hi,
I've now spent hours looking at this and no joy, wondering if any of you guys can help.
I am passing an object that uses dynamically allocated memory into a method, but for some reason when it finishes my original object has changed/been destroyed.
header file:
class Multiset
{
public:
Multiset(); //default constructor for multiset
virtual ~Multiset(); //default destructor for multiset
Create(int newsize); //Creates an array of a given size.
virtual bool AddElement(int ElementValue,int Position); //Adds an element to a given position in the array.
int &ReadElement(int index); //Returns Address of element
bool IsMember(int Element2Check); //Checks if a given element is a member of the multi/set
int GetCardinality(); //Returns the number of elements in a multi/set
bool IsSubset(Multiset MSet2Check); //Returns whether the current multi/set is a Subset of a given multi/set
protected: //Only derived classes should have access
int *data; //Pointer to int
int size; //Holds the size of the Array
bool ArrayExists;
};
Implementation of Class MultiSet:
/////////////////////////////////////////////////////////////////////////////////////
// Construction/Destruction
/////////////////////////////////////////////////////////////////////////////////////
Multiset::Multiset()
{
ArrayExists = false; //Memory has not yet been allocated for the array.
}
Multiset::~Multiset()
{
ArrayExists = false; //Memory has been de-allocated for the array.
delete[] data;
}
/////////////////////////////////////////////////////////////////////////////////////
//
// Create()
// Method to create an array of a given size. (Largest size is determined by int)
// Passed IN : Int Size (Size of array to be created)
// Passed OUT: Nothing
//
/////////////////////////////////////////////////////////////////////////////////////
Multiset::Create(int newsize)
{
// set size to newsize and allocate memory
size = newsize;
data = new int[size];
// returns 0 if new failed, otherwise returns 1
if (data == NULL){
cout << "Not enough memory available";
exit(-1);
}
else
ArrayExists = true;
}
//Multiset object passed in to this method,original gets destroyed!!
bool Multiset::IsSubset(Multiset MSet2Check){
int i=0,j=0;
bool FoundInInner = false ;
if (size > MSet2Check.size)
return false; //Cannot be a Subset as it is larger than MSet2Check
else //Work out if it's a subset of MSet2Check
for (i=0; i<size; i++){
for(j=0; j<MSet2Check.size; j++){
if (data[i] == MSet2Check.data[j]){
FoundInInner = true;
MSet2Check.data[j] = Reset;
break;
}
else
FoundInInner = false;
}
if (FoundInInner == false)
return false;
else
{
j = 0; //j counter is reset to 0 for next i loop
if (i == (size - 1))
return true;
}
}
}
Sorry for the big block of code guys, basically when I use mySet1.IsSubset(mySet2) the original mySet2 values all change on leaving the method IsSubset. It is calling the destructor on leaving and then I think it is killing my original object,
How do I get it to leave my passed in object alone!!??
Thanks for your time guys.