M
Mark Black
I have a program which downloads data from a binary file and converts
into text. A class or CRecord holds basic data.
I want to create objects on the fly dynamically and link the objects
in a linked list.
Problem I have is in creating the new objects!
I know where I am going wrong (when trying to create a deep copy as
opposed to a shallow copy via the copy Constructor. Problem is I have
not worked out how I best rectify this issue. Any helpers please ?
#include <iostream.h>
#include <stdlib.h>
class CRecord
{
public:
int mDataLength;
CRecord* mpHead;
CRecord* mpNext;
void setmpHead()
{
mpHead = this;
};
CRecord* returnmpHead()
{
return mpHead;
};
CRecord()
{
cout << " Constructor Called!" << endl;
mpHead = NULL;
mpNext = NULL;
setmpHead();
};
CRecord(const CRecord&)
{
cout << "Copy Constructor Called!" << endl;
mpHead = new CRecord;
};
~CRecord(){ cout << " Destructor called!" << endl;};
};
CRecord* pHead = NULL;
CRecord* pAdd = NULL;
CRecord* pCurrent = NULL;
int main()
{
CRecord block;
block.setmpHead();
pHead = block.returnmpHead();
for(int i = 0; i < 5; i++)
{
CRecord(const CRecord& rhs);
rhs.setmpHead();
rhs.returnmpHead();
rhs.mpNext = NULL;
pAdd = rhs.mpHead; // memory is a shallow copy !!! //ERROR!
pCurrent = pHead;
while(pCurrent->mpNext != NULL)
{
pCurrent = pCurrent->mpNext;
}
pCurrent->mpNext = rhs.mpHead;
}
return 0;
}
into text. A class or CRecord holds basic data.
I want to create objects on the fly dynamically and link the objects
in a linked list.
Problem I have is in creating the new objects!
I know where I am going wrong (when trying to create a deep copy as
opposed to a shallow copy via the copy Constructor. Problem is I have
not worked out how I best rectify this issue. Any helpers please ?
#include <iostream.h>
#include <stdlib.h>
class CRecord
{
public:
int mDataLength;
CRecord* mpHead;
CRecord* mpNext;
void setmpHead()
{
mpHead = this;
};
CRecord* returnmpHead()
{
return mpHead;
};
CRecord()
{
cout << " Constructor Called!" << endl;
mpHead = NULL;
mpNext = NULL;
setmpHead();
};
CRecord(const CRecord&)
{
cout << "Copy Constructor Called!" << endl;
mpHead = new CRecord;
};
~CRecord(){ cout << " Destructor called!" << endl;};
};
CRecord* pHead = NULL;
CRecord* pAdd = NULL;
CRecord* pCurrent = NULL;
int main()
{
CRecord block;
block.setmpHead();
pHead = block.returnmpHead();
for(int i = 0; i < 5; i++)
{
CRecord(const CRecord& rhs);
rhs.setmpHead();
rhs.returnmpHead();
rhs.mpNext = NULL;
pAdd = rhs.mpHead; // memory is a shallow copy !!! //ERROR!
pCurrent = pHead;
while(pCurrent->mpNext != NULL)
{
pCurrent = pCurrent->mpNext;
}
pCurrent->mpNext = rhs.mpHead;
}
return 0;
}