包括嵌套类

发布于 2024-10-15 18:23:54 字数 981 浏览 0 评论 0原文

我有一个容器类,其中有多个迭代器作为嵌套类。结构类似于:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

失而复得 2024-10-22 18:23:54

不要将嵌套类声明放在单独的头文件中。这是糟糕的设计,并且会让其他必须维护你的代码的人感到困惑。如果嵌套类太大,则取消嵌套并将其放入自己的编译单元(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).

め可乐爱微笑 2024-10-22 18:23:54

在单独的文件中添加嵌套类实现很容易。我一直这样做,但您仍然需要在其所有者中声明嵌套类。执行以下操作:

    class Grid
    {
    protected:
        class Iterator;
        class MoreIterator;
    };

然后,在单独的 cpp 或头文件中,您需要按以下方式实现嵌套类:

class Grid::Iterator
{
    Iterator(Grid* g) : grid(g){}
    Grid* grid;
}
class Grid::MoreIterator : public Iterator
{
}

最酷的一点是 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:

    class Grid
    {
    protected:
        class Iterator;
        class MoreIterator;
    };

Then in a you separate cpp or header file you will need to implement your nested classes the following way:

class Grid::Iterator
{
    Iterator(Grid* g) : grid(g){}
    Grid* grid;
}
class Grid::MoreIterator : public Iterator
{
}

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文