Creating Objects Dynamically.

Discussion in 'C++' started by Mark Black, Jul 4, 2004.

  1. Mark Black

    Mark Black Guest

    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;
    }
    Mark Black, Jul 4, 2004
    #1
    1. Advertising

  2. Mark Black

    David Harmon Guest

    On 4 Jul 2004 08:51:02 -0700 in comp.lang.c++,
    (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?
    David Harmon, Jul 4, 2004
    #2
    1. Advertising

  3. Mark Black

    David Harmon Guest

    David Harmon, Jul 4, 2004
    #3
  4. On 4 Jul 2004 08:51:02 -0700, 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.
    >
    > 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
    John Harrison, Jul 5, 2004
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?UmV6YSBOYWJp?=

    Load User Control Dynamically, Cast object dynamically

    =?Utf-8?B?UmV6YSBOYWJp?=, Mar 4, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    6,719
    Matt Berther
    Mar 5, 2005
  2. =?Utf-8?B?UmV6YSBOYWJp?=

    Load User Control Dynamically, Cast object dynamically

    =?Utf-8?B?UmV6YSBOYWJp?=, Mar 4, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    6,353
    Scott Allen
    Mar 4, 2005
  3. Jeffrey Todd
    Replies:
    1
    Views:
    4,101
    Peter Blum
    Jun 2, 2005
  4. 7stud
    Replies:
    11
    Views:
    684
    Dennis Lee Bieber
    Mar 20, 2007
  5. News123
    Replies:
    7
    Views:
    378
    Dave Angel
    Jun 29, 2009
Loading...

Share This Page