Creating Objects Dynamically.

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;
}
 
D

David Harmon

On 4 Jul 2004 08:51:02 -0700 in comp.lang.c++,
(e-mail address removed) (Mark Black) wrote,
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.

In your situation I would certainly use std::list, and make the record
class very simple. Is there some reason that would not be satisfactory?
 
J

John Harrison

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 ?

The code is not even close to being correct, so its not easy to give quick
advice.

I guess the main problem is the design. You are trying to write a linked
list class, so where are the 'add_to_list', 'remove_from_list',
'list_size', 'iterate_thru_list' etc. etc. methods? This is simple stuff
that every linked list should have. Clearly you are concentrating solely
on the implementation of your linked list (and getting that wrong) instead
of thinking about the linked list interface.

Now here's the thing, if you concentrate on the interface, the programming
becomes easier. Good design not only makes using your linked list class
easier, it also makes programming it easier.

I don't have time to go though an entire linked list design for you (try a
web search or a good book for that) but here's a start.

The key realisation is that your node class (what you've called CRecord)
is an implementation detail and should *not* be exposed in the interface.
The user of your linked list isn't interested in nodes, they're interested
in adding data to a list. So you need a class called CList which has
methods like add, remove etc. but none of these methods have CRecords as
parameters or return types. Of course *internally* CList will be creating
and deleting CRecords, but that is not exposed in the interface. Something
like this (this assumes the data on the list is integers)

class CList
{
public:
CList();
CList(const CList&);
void add_head(int data);
void add_tail(int data);
void remove_head();
void remove_tail();
int get_size() const;
int get_current() const;
void next();
private:
CRecord* pHead;
CRecord* pAdd;
CRecord* pCurrent;
};

See? Not a CRecord in sight, its all happening behind the scenes.

john
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top