Reduce permissions for individual nested classes

I have this (simplified) situation:

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

I don't want to clutter up class definitions, and I decide to write only method declarations within the classes themselves. Later (waaay below), when I want to define, say, a copy destination statement as follows:

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

I have to deal with these nasty permissions. I am wondering if there is a way to shorten them , because using and typedef , as I know them, do not offer me anything.

EDIT: since this is not CodeReview, but @Yulian requested clarification, here's a short version:

I am doing an iterative implementation of Red-Black Tree. The mentioned class Iterator designed to move in order (therefore, it depends on the order), and class Stack is its utility class. In this short program, only class Tree uses Iterator , and only Iterator uses Stack .

After reminding @Yulian, I remembered that it would be more object-oriented if the mentioned classes were separately defined (perhaps even as templates), but this is a small stand-alone program, and I'm trying to save it this way.

EDIT: stand-alone also means that it is an isolated, single-file program, so no .h files or external code to reuse. What for? Because ACADEMIA (and related arbitrary restrictions).

+6
source share
3 answers

You can completely exclude region permissions using using or typedef . But not in the traditional way, because your nested classes are declared private. Thus, you will need to use the extra using in the public section of each nested class to "expose" them. Unfortunately, this violates their "privacy" :

 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

However, you could remove the levels of the region (except for the outer, i.e. Tree:: :) without exposing the private nested classes as follows:

 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

+3
source

First of all, it’s good to explain what you would like to achieve and how you think this method is good for your task. IMHO, the best approach is to create separate classes that are not nested, and use them inside three classes. This approach is called "HAS A". It is cleaner, easier to maintain and understand from others. If you provide more detailed information, we can develop a better design.

0
source

You can let the preprocessor help you:

 #define foobar Tree::Iterator::Stack foobar& foobar::operator = (const Stack& p_stack) { // ... } 
-2
source

All Articles