包括嵌套类
我有一个容器类,其中有多个迭代器作为嵌套类。结构类似于:
class Grid
{
protected:
class Iterator
{
Iterator(Grid* g) : grid(g){}
Grid* grid;
}
class MoreIterator : public Iterator
{
}
}
现在我想将迭代器移动到它们自己的头文件中,以清理容器代码。
class Grid
{
protected:
#include "griditerators.h"
}
到目前为止,编译没有错误。但是:
在 QtCreator 中,这些行
Iterator(Grid* g) : grid(g){}
Grid* grid;
被标记为错误,告诉我“网格不是类型名称”。
我认为我可以通过在 griditerator.h 文件中进行前向声明来解决这个问题:
class Grid;
class Iterator
{
Iterator(Grid* g) : grid(g){}
Grid* grid;
}
但这给了我一个编译错误:类 Grid 与声明它的类具有相同的名称。
将前向声明替换为 #include“grid.h” 作品。但我不知何故觉得这很丑陋。
所以我有两个工作选择。一个在我的 IDE 中显示丑陋的错误,另一个我只是不太喜欢。
我尝试了其他变体,其中包含不在封闭类中,但由于不同的原因而未能编译。
所以我的问题是:是否有任何“最佳实践”或包含模式来处理太大而无法将它们保留在封闭类文件中的嵌套类?
例如,是否有一种方法可以声明一个嵌套类,例如:
class Grid::Iterator
I have a container class with several iterators as nested classes. The structure is something ilke this:
class Grid
{
protected:
class Iterator
{
Iterator(Grid* g) : grid(g){}
Grid* grid;
}
class MoreIterator : public Iterator
{
}
}
Now I wanted to move the iterators in a header file of their own, to clean up the containers code.
class Grid
{
protected:
#include "griditerators.h"
}
So far this compiles without errors. But:
In QtCreator the lines
Iterator(Grid* g) : grid(g){}
Grid* grid;
are marked as errors, telling me "Grid is not a type name".
I thought I could solve that with a forward declaration in the griditerator.h file:
class Grid;
class Iterator
{
Iterator(Grid* g) : grid(g){}
Grid* grid;
}
But that gives me a compile error: Class Grid has the same name as the class in which it is declared.
Replacing the forward declaration with
#include "grid.h"
works. But I somehow think that's ugly.
So I have two working options. One shows ugly errors in my IDE, the other I just don't like that much.
I tried other variations, where the include isn't within the enclosing class, but that failed to compile for different reasons.
So my question is: Are there any "best practices" or include patterns to handle nested classes that are too large to keep them within their enclosing class file?
For example would there be a way to declare a nested class like:
class Grid::Iterator
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不要将嵌套类声明放在单独的头文件中。这是糟糕的设计,并且会让其他必须维护你的代码的人感到困惑。如果嵌套类太大,则取消嵌套并将其放入自己的编译单元(h/cpp 组合)中。
Don't put the nested class declaration in a separate header file. It's bad design and will confuse the hell out of anyone else who has to maintain your code. If the nested class is too big, unnest it and put it in it's own compilation unit (h/cpp combo).
在单独的文件中添加嵌套类实现很容易。我一直这样做,但您仍然需要在其所有者中声明嵌套类。执行以下操作:
然后,在单独的 cpp 或头文件中,您需要按以下方式实现嵌套类:
最酷的一点是 Grid 类不需要知道两个嵌套类的详细信息,而是需要知道两个嵌套类的详细信息。嵌套类知道 Grid 类的详细信息,并且有权访问 Grid 类中的私有数据,就像 Grid 类成员的任何方法一样。
It is easy to add nested class implementation in a separate file. I do it all the time, but you will still need to declare the nested class in it's owner. Do the following:
Then in a you separate cpp or header file you will need to implement your nested classes the following way:
The cool thing about this is that class Grid does not need to know the details of the two nested classes, but the two nested classes know the details of class Grid and will have access rights to the private data in class Grid just like any method that is a member of class Grid.