我可以使用STL容器来管理不完整的类对象吗? [英] Can I manage incomplete class objects using STL containers?

查看:103
本文介绍了我可以使用STL容器来管理不完整的类对象吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C ++ 11中,声明一个仍然是不完整类型的类的向量肯定是无效的?我想我只能使用不完整的类型作为指针,引用,返回类型或参数类型。搜索(1)不完全类型的向量建议我不完整类型的容器应该是一个错误(我使用g ++版本4.8.1。)。然而,下面的代码在我的系统上编译罚款:

In C++11, it is definitely invalid to declare a vector of a class which is still an incomplete type right? I thought I could only use incomplete types as pointers, references, return types or parameter types. Searching (1) for "vector of incomplete type" suggests to me that containers of incomplete types should be an error (I'm using g++ version 4.8.1.). However the following code compiles fine on my system:

#ifndef SCREEN
#define SCREEN
#include <string>

using namespace std;

class Screen;

class Window_mgr{
public:
    typedef vector<Screen>::size_type screenindex;
    void clear(screenindex);
private:
    vector<Screen> screens;
};

class Screen{
    friend void Window_mgr::clear(screenindex);
public:
    typedef string::size_type pos;
    Screen() = default;
    Screen(pos ht, pos wt): height(ht), width(wt), contents(ht*wt, ' ') { }
    Screen(pos ht, pos wt, char c): height(ht), width(wt), contents(ht*wt, c) { }

private:
    pos height = 0, width = 0;
    pos cursor = 0;
    string contents;

};

inline void Window_mgr::clear(screenindex i){
    Screen& s = screens[i];
    s.contents = string(s.height*s.width, ' ');
}

#endif // SCREEN

Window_mgr声明一个Screens的向量,它仍然是一个不完整的类型。在我的例子中的这些类实际上是基于C ++ Primer的第7.3章有任何它。一个问题要求我定义我自己的Screen和Window_mgr版本,其中成员函数clear是Window_mgr的成员和Screen的朋友。除了Window_mgr也意味着包含一个屏幕的向量。

despite the fact Window_mgr declares a vector of Screens, which is still an incomplete type. These classes in my example are actually based on Chapter 7.3 of C++ Primer for any that have it. A question asked me to define my own versions of Screen and Window_mgr in which the member function clear is a member of Window_mgr and a friend of Screen. Except Window_mgr is also meant to contain a vector of Screens.

如果创建一个不完整类型的向量是无效的,我该如何使用转发声明?如果我在Window_mgr中有一个屏幕向量,那么它的类定义必须在类Screen已经被定义之后。除了Screen必须有一个明确的成员函数Window_mgr的朋友声明,但下面的重新排列是一个错误,因为Screen使用范围操作符在一个不完整的类型;

If creating a vector of an incomplete type IS invalid, how exactly would I do this using forward declarations? If I have a vector of screens in Window_mgr then its class definition must come after class Screen has already been defined. Except Screen must have a friend declaration of the clear member function of Window_mgr, but the following re-arrangement is an error because Screen uses the scope operator on an incomplete type;

class Window_mgr;

class Screen{
    friend void Window_mgr::clear(screenindex);
public:
    typedef string::size_type pos;
    Screen() = default;
    Screen(pos ht, pos wt): height(ht), width(wt), contents(ht*wt, ' ') { }
    Screen(pos ht, pos wt, char c): height(ht), width(wt), contents(ht*wt, c) { }

private:
    pos height = 0, width = 0;
    pos cursor = 0;
    string contents;

};

class Window_mgr{
public:
    typedef vector<Screen>::size_type screenindex;
    void clear(screenindex);
private:
    vector<Screen> screens;
};

inline void Window_mgr::clear(screenindex i){
    Screen& s = screens[i];
    s.contents = string(s.height*s.width, ' ');
}

我可以想到的唯一方法是将memberfunction friend声明替换为只是朋友声明类

The only way I could think would be to replace the memberfunction friend declaration with a just friend declaration of the class

class Screen{
friend class Window_mgr;
// other stuff
}

但这不是什么问题我。

but that's not what the question wants of me.

推荐答案

标准容器目前不支持不完整类型;参见标准容器模板可以使用不完整类型进行实例化吗? - 实现可以选择支持不完整的类型作为扩展,但是这使得你的代码不可移植。

Incomplete types are currently not supported by standard containers; cf. Can standard container templates be instantiated with incomplete types? - that said, implementations can choose to support incomplete types as an extension, but that makes your code non-portable.

论文 n4371标准容器的最小不完全类型支持,修订版2 已并入最近的草稿( n4527 ) C ++标准,所以除非意外,非常有可能不完整的类型将支持向量 list code> forward_list 在C ++ 17中。

The paper n4371 Minimal incomplete type support for standard containers, revision 2 has been incorporated into the most recent draft (n4527) of the C++ Standard, so barring the unexpected it is very likely that incomplete types will be supported for vector, list and forward_list in C++17.

满足C ++ Primer中的要求而不依赖于实现扩展或C ++ 17:

There is one way to satisfy the requirements in "C++ Primer" without depending on implementation extension or C++17:


  1. clear Window_mgr 的成员函数;

  2. Window_mgr :: clear code>是 Screen 的朋友;

  3. Window_mgr 包含向量屏幕 s:

  1. clear is a member function of Window_mgr;
  2. Window_mgr::clear() is a friend of Screen;
  3. Window_mgr is contains a vector of Screens:

c>屏幕 Window_mgr 的嵌套类:

class Window_mgr{
public:
    typedef std::size_t screenindex;
    void clear(screenindex);

    class Screen{
        friend void Window_mgr::clear(screenindex);
    public:
        // ...
    };

// ...
private:
    vector<Screen> screens;
};

即使在这里,我不得不调整类型 screenindex 打破依赖性循环。

Even here I've had to adjust the definition of type screenindex to break the dependency cycle.

这篇关于我可以使用STL容器来管理不完整的类对象吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆