samedi 11 mars 2017

How do I not call a constructor of an object that is located in a node of a linked list?

So I have a class, lets call it Cow. This cow has an age and some other variables like a static number of Cows alive variable.

Well I want to make a linked list, and I made the class for it and the node structure inside of the linkedlist class, looks a bit like this.

struct node{
        Cow data;
        node* next;
};

Then there's my addNode function to add a node.

void List::addNode(Cow newData)
{
    //Creates a Cow object that will skew counters. BELOW.
    node* n = new node;
    n->data = newData;
    n->next = NULL;
    if(head == NULL){
        head = n;
    }else{
        curr = head;
        while(curr->next != NULL){
            curr = curr->next;
        }
        curr->next = n;
    }

}

With the line node* n = new node, it'll create a new Cow object, which calls the Cow constructor, which increments the number of Cows alive static variable.

Simply, my question is... How would I go about not calling the constructor for that Cow object when the node is first created, so I can fill it up with the newData object instead. Therefore not messing up my counter, which increments in the constructor?

Aucun commentaire:

Enregistrer un commentaire