C++ How may I return an array class member of a nested class?

394 Views Asked by At

This is not a real question, since I've already solved the problem myself, but I still need some clarifications about the mechanism behind assigning an array's address to a pointer of the same type when the array is a class member of a nested class.

The following code is fully functioning, although it may lack some error_check. It is only meant to show how I made my (real) program work.


HEADER (linkedList)

class linkedList
{
public:
    linkedList();
    ~linkedList();
    int* getArray();
    void forward();

private:
    class listNode
    {
    public:
        listNode();
        ~listNode();

        friend class linkedList;

    private:
        int array[3];
        listNode* next;
    };

    listNode *first;
    listNode *current;
};

CPP (linkedList)

linkedList::linkedList()
{
    first = new listNode;
    current = first;
}
//~~~~~~~~~~~~
linkedList::~linkedList()
{
    delete first;
    first = 0;
    current = 0;
}
//~~~~~~~~~~~~
int* linkedList::getArray()
{
    if (current)
    {
        return &(current->array[0]);
    }
}
//~~~~~~~~~~~~
void linkedList::forward()
{
    if (current->next)
    {
        current = current->next;
    }
}

//-------------------------
//-------------------------
//-------------------------
linkedList::listNode::listNode()
{
    next = 0;
    for (int i = 0; i < 3; i++){array[i]=((i+1)*3);}
}
//~~~~~~~~~~~~
linkedList::listNode::~listNode()
{

}

CPP (main)

#include <iostream>
#include "linked_list.h"

using namespace std;

int main()
{
    linkedList list;

    int *myArray;

    myArray = list.getArray();

    for (int i = 0; i < 3; i++){cout << myArray[i] << " ";}/**/cout << "\n\n";

    return 0;
}

The real program is meant to move through a linked list made of nodes which contain 3 integer values in an array of int type, retrieve the three values and use them as parameters for some other functions.

Now, to do so I have to return the address to the first element of the array contained in the node through an accessor. Apparently, the only way to do it is by returning the reference to the first element of the array in the node to which the linkedList's member variable current points to:
return &(current->array[0]);.

Why?

I've got to this solution through trial and error with very little knowlegde of the reasons that brought me to build this expression as it is. Usually, when you want to assign the address of an array to a pointer, you just do so:

int main()
{
    int array[3];
    int* pArray;

    pArray = array;
}

And that's it, because the name of the array itself is enough to retrieve the address of its first element.

The exact same result can be achieved by doing this (tested):

int main()
{
    int array[3];
    int* pArray;

    pArray = &(array[0]);
}

Both methods are also valid when the accessor returns the address from a member variable of its own class.

But why, when accessing the member variable of a nested class, I'm forced to use the second method? What are the logic stages that make it the only viable method?

1

There are 1 best solutions below

2
eerorika On

But why, when accessing the member variable of a nested class, I'm forced to use the second method?

You aren't:

return current->array;

and

return &(current->array[0]);

Both do the same thing when the return type is int*. You aren't forced to use the second way.

Also, there's a bug in getArray. You don't return anything if current is null.

To be pedantic...

Apparently, the only way to do it is by returning the reference to the first element of the array in the node to which the linkedList's member variable current points to: return &(current->array[0]);.

You're returning the address i.e. a pointer. Reference means something else.