Can I manage partial class objects using STL containers?

In C ++ 11, is it definitely wrong to declare a vector of a class that is still an incomplete type of right? I thought I could use incomplete types like pointers, references, return types, or parameter types. A search (1) for an "incomplete type vector" tells me that containers of incomplete types should be an error (I am using g ++ version 4.8.1.). However, the following code compiles 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 , - . 7.3 ++ Primer , . Screen Window_mgr, clear Window_mgr Screen. , Window_mgr .

IS , ? Window_mgr, , Screen . , - Window_mgr, , ;

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

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

, .

+2
2

; ? - , .

n4371 , 2 (n4527) ++, , , , vector, list forward_list ++ 17.


"++ Primer" ++ 17:

  • clear - Window_mgr;
  • Window_mgr::clear() Screen;
  • Window_mgr Screen s:

Screen 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, .

+3

: . , STL. , - , , .

, (1), . ( ) .

, :

  • STL
  • STL
  • - , . :

, , , : , Standard Library .

, , . , . , :

class Foo
{
   std::vector<Bar> myVector;
}

class Bar
{
   std::vector<Foo> myVector;
}

, . . (std::shared_pointer ). , , . .

+3

Source: https://habr.com/ru/post/1652108/


All Articles