Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Shorter scope resolutions for private nested classes

I have this (simplified) situation:

class Tree {
    class Iterator {
        class Stack {
            // ...
        }
    public:
        // ...
    }
public:
    //...
}

I don't want to clutter the classes' definitions and I decide to write only method declarations inside classes themselves. Later on (waaay down below) when I want to define, say, copy assignment operator like this:

Tree::Iterator::Stack& Tree::Iterator::Stack::operator = (const Stack& p_stack) {
// ...
}

I have to deal with these nasty scope resolutions. I'm wondering if there's a way to shorten them, because using and typedef, as I know them, don't offer me anything.

EDIT: Since this is not CodeReview, and @Yulian requested clarification, here's the short version:

I'm making an iterative Red-Black Tree implementation. Mentioned class Iterator is for post-order traversing (so it's post-order-specific), and class Stack is its utility class. In this short program, only class Tree uses the Iterator, and only Iterator uses Stack.

After @Yulian's reminder, I recalled that it would be way more object-oriented if the mentioned classes were separately defined (maybe even as templates), but this is a small, self contained program and I'm trying to keep it that way.

EDIT: Self-contained also means that it's an isolated, single-file program, so no .h files or external code re-using whatsoever. Why? Because ACADEMIA (and associated arbitrary restrictions).

like image 864
Stefan Stanković Avatar asked Nov 12 '15 20:11

Stefan Stanković


People also ask

What is the scope we use for nested class?

A class can be declared within the scope of another class. Such a class is called a "nested class." Nested classes are considered to be within the scope of the enclosing class and are available for use within that scope.

Is nested class good practice?

Nested Class can be used whenever you want to create more than once instance of the class or whenever you want to make that type more available. Nested Class increases the encapsulations as well as it will lead to more readable and maintainable code.

What is a nested class how can we access members of a nested class?

A nested class is a class which is declared in another enclosing class. A nested class is a member and as such has the same access rights as any other member. The members of an enclosing class have no special access to members of a nested class; the usual access rules shall be obeyed.

How many types of nested classes are available in C++?

Java programmers have several different kinds of nested classes to choose from, but C++ has only one. A C++ nested class is like a static member class in Java.


1 Answers

You could totally eliminate scopes resolutions with a using or typedef. But not with the traditional way because your nested classes are declared private. So you would have to use additional using in the public section of each nested class to "expose" them. Unfortunately, this breaks the "privateness" of them:

class Tree {
    class Iterator {
        class Stack {
            Stack& operator = (const Stack& p_stack);
        };
    public:
      using Stack_Out = Stack;

        // ...
    };
public:
    using Iterator_Out = Iterator::Stack_Out;
    //...

};

using Stack = Tree::Iterator_Out;

Stack& Stack::operator = (const Stack& p_stack) {
// ...
}

LIVE DEMO

You could however, remove scope levels (except for the outer one, i.e., Tree::) with out exposing the private nested classes in the following way:

class Tree {
    class Iterator {
        friend class Tree;
        ^^^^^^^^^^^^^^^^^^
        class Stack {
            Stack operator = (const Stack& p_stack);
        };
    public:

        // ...
    };

    using Stack = Iterator::Stack;
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

public:

}; 

Tree::Stack Tree::Stack::operator = (const Stack& p_stack) {
^^^^^^^^^^^ ^^^^^^^^^^^
}

LIVE DEMO

like image 83
101010 Avatar answered Nov 15 '22 00:11

101010