Class won't pass by reference

The following code is printing out 343. I don't understand why it isn't taking in currentDirectory by reference and updating what it's pointing to to a subdirectory of the root instance.

I would expect it to output 344.

#include <iostream>

using namespace std;

class Directory;

struct Node{
    Node* prev;
    Node* next;
    Directory* data;
};

class Directory{
    public:
        int testValue;
        Node* subDirectories;

        void addSubdirectory(int testValue){
            Directory* newDirectory = new Directory(testValue);

            Node* elem = new Node;
            elem->prev = NULL;
            elem->data = newDirectory;

            elem->next = NULL;
            subDirectories = elem;
        }

        Directory(int x){
            testValue = x;
        }
};

void testFunction(Directory* x){
    x = x->subDirectories->data;
    cout << x->testValue;
}

int main(){
    Directory* root = new Directory(3);
    root->addSubdirectory(4);

    Directory* currentDirectory = root;

    cout << currentDirectory->testValue;

    testFunction(currentDirectory);

    cout << currentDirectory->testValue;
}

I've tried a simplified example that works fine:

#include <iostream>

using namespace std;

class Directory;

class Directory{
    public:
        int testValue;

        Directory(int x){
            testValue = x;
        }
};

void testFunction(Directory* x){
    x->testValue = 4;
    cout << x->testValue;
}

int main(){
    Directory* root = new Directory(3);

    Directory* currentDirectory = root;

    cout << currentDirectory->testValue;

    testFunction(currentDirectory);

    cout << currentDirectory->testValue;
}

This prints out 344 as expected. This one is passing it in by reference.

Answers


Your problem is that you are passing by value not by reference. The instance that is being passed to "testfunction()" is "currentDirectory" which is a pointer to object. So if you intend to pass by reference it should be like this:

void testFunction(Directory* &x){
    x = x->subDirectories->data;
    cout << x->testValue;
 }

You are making a copy of type Directory* and passing it into testFunction as an argument. That copy, x, is not a reference to the pointer root.

This is probably what you want:

void testFunction(Directory* &x){
    x = x->subDirectories->data;
    cout << x->testValue;
}

Or if you prefer the double pointer route you would have:

void testFunction(Directory** x){
    (*x) = (*x)->subDirectories->data;
    cout << (*x)->testValue;
}

// the call turns into
testFunction(&currentDirectory)

Your simplified example works because you are not actually assigning to the variable that is passed-by-value, x:

void testFunction(Directory* x){
    x->testValue = 4; // assigning pointed-to value, not pointer
    cout << x->testValue;
}

Long story short, pointer-to-type is a type in and of itself too. Passing a pointer by reference takes the same changes as any other type.


Alternately, you could create an object of the class rather than a pointer. Note, this would require you to add a copy constructor (and, hopefully, a destructor, and overloaded operator=).

Directory root(3);
root.addSubdirectory(4);

//Assuming you overload operator=
Directory currentDirectory = root;

cout << currentDirectory.testValue;

testFunction(currentDirectory);

Where

void testFunction(Directory & x);

Need Your Help

When is [NSWindowDelegate windowWillClose] called?

objective-c cocoa

I have observed that windowWillClose is called when the user closes a window but is NOT called when the window is closed because of app termination.

Use textscan to read datablock

matlab file-io textscan

How to extract the "mean" and "depth" data like the following of each month?