0

Hello Word

I have a node struct as follows..

typedef struct Node
{
    Node* left = nullptr;
    Node* right = nullptr;
    string word;
    int lineNumber = 0;
    int count = 0;
};

The goal here is to connect this node to other nodes ( I know very basic ) but I seem to be missing some subtleties that I may be overlooking

Here is my attempt

cout << "************Testing Root Node**********************\n" << endl;
        1   Node *newNode = new Node;       
        2   Node  temp = *newNode;
        3   temp.word = word;
        4   temp.lineNumber = lineNumber;
        5   wtree.setRoot(newNode); 


        6   Node temp2 = *newNode;
        7   cout << "Testing temp.word = " <<temp.word << endl;
        8   cout << "Testing temp2.word = " << temp2.word << endl;

        9  Node *test = wtree.getRoot();
        10  Node test2 = *test;

        cout << "RootNode.getWord() should be CATS it is...\n\n\ " << test2.word << endl;
        cout << "************End of Root Node Test******************\n\n\n\n" << endl;

My Analysis

I believe that line 2 -4 are wrong but I just do not know how to fix it/

My output is this

Testing temp.word = cats
Testing temp2.word = _____(blank)
RootNode.getWord() should be CATS it is... _______ (blank)

This output tells me that the newNode is not being changed at all. So my temp.word = word is not doing the behavior I expect.

Can someone tell me how I should be handling this situation properly ? Thanks in advance

Please let me know if I have not been clear and I will gladly provide more details

I also tried

*newNode.word = word 

But this gave me compile issues

Bonus question

Does the new operator do the same thing as malloc does ?

I am fairly new to C++ but I believe it does

Rstack
  • 45
  • 1
  • 10
  • `This output tells me that the newNode is not being changed at all.` That's because you aren't changing `newNode` anywhere in your code, `temp` and `temp2` are initialized as a copy of `newNode`, changing the members of `temp` or `temp2` won't modify `newNode`. Your "bonus" unrelated question has a [number](http://stackoverflow.com/questions/240212/what-is-the-difference-between-new-delete-and-malloc-free) of answers. – user657267 Sep 16 '14 at 00:22

2 Answers2

1

temp is a copy of newNode. So when you assign values to it, you aren't affecting newNode.

If you want to set newNode directly, then use something like:

newNode->word = word;

You don't need all of those copies (like temp2), just use -> to access member variables from pointers to structs or classes.

Edit: Bonus question: new is similar to malloc. It allocates memory for the object. However, new also calls the object's constructor, which allows objects to initialize themselves.

The Dark
  • 8,453
  • 1
  • 16
  • 19
  • @RyanThomas Incidentally it seems you were originally thinking of those copies as _references_. While it's not required in this case (using pointer accessors is the way to go), changing the declarations to `Node & temp = *newNode`, `Node & temp2 = *newNode` and `Node & test2 = *test` would also have given the behaviour you were expecting. – etheranger Sep 16 '14 at 01:18
0

Node temp = *newNode; // This line is creating a new object and not pointing to the object of newNode

If you want to reflect your changes to newNode when you make changes on temp, you can use reference

Node &temp = *newNode;

By this way, temp is an alias of newNode instead of a brand new object.

cck3rry
  • 191
  • 2