user3596926
user3596926

Reputation: 1

How do you access inner classes within another class?

I have a Node class in another class which is under private in the Structure class which is the main class I am working with. I am then in another .cpp file which implementing the functions that are in the structure.h file which contains the Structure class and the Node class nested inside the Structure class under private.

How do I access that Node class when creating a return type of Node*. I have been messing around with it for awhile and nothing will work. It allows me to return an int, bool, double, but not a Node*.

"Structure.h"
template <class T> 
class Structure
{
    public:
        // Structure functions();
    private:

    class Node
{
    public::
        // Node functions and variables;
}
    // Structure functions_private();
}

Upvotes: 0

Views: 54

Answers (2)

Mike Seymour
Mike Seymour

Reputation: 254431

Within the class definition, you can simply refer to the type as Node.

Outside, you need the qualified name, along with typename if it depends on a template parameter:

Structure<int>::Node * some_function();  // not dependent, no typename

template <typename T>
typename Structure<T>::Node * some_template(); // dependent, needs typename

Upvotes: 1

qeadz
qeadz

Reputation: 1516

Do you mean something like:

template< class T >
typename Structure<T>::Node* Structure<T>::MyMemberFunction()
{
    return new Node();
}

As an example.

Also you'll need to put this in your header file not in the source.

Upvotes: 1

Related Questions