Thread: newbie - copy constructor - trouble

  1. #1

    newbie - copy constructor - trouble

    Because i need to traverse through the nodes, i create a node (s) that keep assigning itself the next node till the desired one is reached. BUT TOP IS BEING CHANGED ALSO !!! I think i need a copy constructor for this, but how ???????

    Creation : node *s = top; // top is a node also

    struct node{
    Field *fld;
    node *next;


    node (const node& incoming){


  2. #2
    p.s. Field is an abstract base class

    (you can't create it, but a pointer to it may well be used)

  3. #3
    ¡Amo fútbol!
    Join Date
    Dec 2001
    To make the copy construtor, assign all of the data in the incoming node into your new node. Say you had the struct point.

    struct point
    {  int x;
        int y;
        point(point &);//copy constructor
    //definition of copy constructor
    point::point(point &incoming)
    {  x=incoming.x;

  4. #4
    that still touches the original memory space.

  5. #5
    Registered User
    Join Date
    Dec 2001
    This is VERY ugly and inefficient way of doing it..

    why not use pointer to traverse the nodes??? saves time (especially if it's recursive) and memory!!.

    are you traversing a linked list or a tree?

    Quidquid latine dictum sit, altum sonatur.
    Whatever is said in Latin sounds profound.

  6. #6
    ¡Amo fútbol!
    Join Date
    Dec 2001
    U. definately has the correct idea here. This is what u need to do:

    1. Create your node class

    2. Give a value to your data and create a new node on the free store that the next node points to

    3. To go through it, assign a traverser node to the head, then if you want to keep going, reassign the traverser to head's next. Continue assigning it to the current node's next to keep going through the list.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. copy = concatenate ?
    By Arruba in forum C Programming
    Replies: 3
    Last Post: 11-03-2006, 04:54 PM
  2. calling copy constructor from template
    By Ancient Dragon in forum C++ Programming
    Replies: 3
    Last Post: 09-28-2005, 01:54 PM
  3. dynamic memory alloccation & returning objects
    By haditya in forum C++ Programming
    Replies: 8
    Last Post: 04-21-2005, 11:55 PM
  4. very newbie question: how to copy files
    By webwesen in forum C Programming
    Replies: 26
    Last Post: 04-25-2002, 03:01 PM
  5. Copy Constructor Help
    By Jubba in forum C++ Programming
    Replies: 2
    Last Post: 11-07-2001, 11:15 AM