我可以避免 Matrix 类迭代器中的循环依赖吗?
我们有两个类:
template<typename T, typename Size, typename Stack, typename Sparse>
class Matrix
Matrix
template<typename T, typename Size>
class Iterator
应该能够返回开始和结束迭代器,并且 Iterator 将保留对 Matrix 的引用以通过其接口访问元素。我们不希望Iterator依赖于Matrix的内部存储来防止耦合。我们如何解决这个循环依赖问题呢?
(内部Storage类具有与Matrix类相同的模板参数以及与Matrix本身相同的访问过程)
We have two classes:
template<typename T, typename Size, typename Stack, typename Sparse>
class Matrix
and
template<typename T, typename Size>
class Iterator
Matrix should be able to return begin and end iterators and Iterator will keep a referrence to the Matrix to access the elements via it's interface. We don't want Iterator to depend on the internal storage of the Matrix to prevent coupling. How can we solve this cyclic dependency problem?
(The internal Storage class has the same template parameters as the Matrix class and the same access procedures as the Matrix itself)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
首先,向前声明 Matrix 类。这允许 Iterator 类查看 Matrix 类的名称,并对其进行指针和引用。 (它还不允许 Iterator 类访问成员数据或调用成员函数。)
然后,定义 Iterator 类。此时它所能做的就是保存指向 Matrix 的引用和指针。 (还不能访问 Matrix 的成员。)
然后定义 Matrix 类(可以访问 Iterator 成员),
然后定义每个类的方法体。此时,两个类的方法就可以访问彼此的成员了。通常,这部分位于 .cpp 文件中,但对于模板,它属于 .h 文件中。
First, forward declare the Matrix class. This allows the Iterator class to see the name of the Matrix class, and make pointers and references to it. (It doesn't allow the Iterator class to access member data yet or call member functions yet.)
Then, define the Iterator class. All it can do at this point is hold references and pointers to the Matrix. (No access to Matrix's members yet.)
Then define the Matrix class (which can access Iterator members)
Then define the method bodies for each class. At this point, the methods of both classes can access each other's members. Usually, this part goes in the .cpp file, but for templates it belongs in the .h file.
在这里使用嵌套类也可能是合适的,并且它可能会减少您需要的模板参数的数量。
Using a nested class might also be appropriate here, and it may cut down on the number of template parameters you need.
为了进行迭代,迭代器通常需要了解它们迭代的内部存储——这种耦合通常是无法避免的。以地图迭代器为例 - 它必须了解地图的内部树结构才能完成其工作。
In order to iterate, iterators typically do need to know about the internal storage they iterate over - this coupling can usually not be avoided. Take a map iterator for example - it is going to have to know about the internal tree structure of the map in order for it to do its job.
您可以前向声明模板。它看起来像这样:
You can forward-declare a template. It looks like this:
在定义
Iterator
模板之前前向声明Matrix
模板。请注意,当您意识到
Iterator
无法引用Matrix
时,您就会碰壁。 >。Forward-declare the
Matrix
template before defining theIterator
template.Mind you, you'll hit a brick wall when you realise that an
Iterator<T, Size>
can't refer to aMatrix<T, Size, Stack, Sparse>
.