私有嵌套类的范围更小的解析 [英] 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) {
// ...
}
我必须处理这些讨厌的范围解决方案。我想知道如果有办法缩短它们,因为使用
和 typedef
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.
编辑:因为这不是CodeReview,而@Yulian要求澄清,这里是短版本:
Since this is not CodeReview, and @Yulian requested clarification, here's the short version:
我在做一个迭代的红黑树实现。提到 class Iterator
用于后序遍历(因此是后序特定的), code>是它的实用程序类。在这个简短的程序中,只有
类Tree
使用 Iterator
,并且只有 Iterator
使用堆栈
。
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
.
@ Yulian的提醒之后,我回忆起,如果所提到的类是单独定义的(也许甚至作为模板),但这是一个小的,自包含的程序,我试图保持这种方式。
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.
编辑:自包含也意味着它是一个孤立的单文件程序,所以没有.h文件或外部代码重用任何。为什么?
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).
推荐答案
因为ACADEMIA (以及相关的任意限制) code>使用或 typedef
。但不是用传统的方式,因为你的嵌套类被声明为私有的。因此,您必须在每个嵌套类的 public
部分中使用使用
来公开它们。不幸的是,这打破了它们的私有性:
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) {
// ...
}
然而,你可以删除范围级别(除了外部一个,即 Tree ::
)暴露私有嵌套类
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) {
^^^^^^^^^^^ ^^^^^^^^^^^
}
这篇关于私有嵌套类的范围更小的解析的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!