The code below has a class Screen. With members: position of cursor, width of screen, height of screen and contents of screen. It has another class Window_mgr which list is collection of screens. Each screen has a particular position in the vector. I will mark out the line in code with an arrow so you need not to go through the complete code.
My question is when we list initialise vector(suppose int) we simply do that by:
std::vector<int> ivec{1, 2, 3, 4};
But in the code below I am initialising vector of object type Screen. What does this means:
VECTOR screens = VECTOR{ {Screen(24, 80, ' '), Screen(32, 56, 'r') }};
I am talking only about the members inside of list initialisation. i.e. Screen(24, 80, ' ') and Screen(32, 56, 'r'). Do they call the version of constructor for Screen class? How is constructor called without creating object. According to me it should have been:
{Screen sr1(24, 80, ' '), Screen sr2(32, 56, 'r')}
Or just
{(24, 80, ' '), (32, 56, 'r')}
I did a search over internet but couldn't get the concept. Thanks
#include <iostream> #include <string> #include <vector> class Screen{ public: using pos = std::string::size_type; Screen() = default; Screen(pos ht, pos wd, char c) : height(ht), width(wd), contents(ht *wd, c) //in-class initializer {} char get() const { return contents[cursor]; } inline char get(pos ht, pos wd) const; Screen &set(char); Screen &set(pos, pos, char); Screen &move(pos r, pos c); void some_member() const; private: pos cursor = 0; pos height = 0, width = 0; std::string contents; mutable size_t access_ctr; }; inline Screen &Screen::set(char c) { contents[cursor] = c; return *this; } inline Screen &Screen::set(pos r, pos c, char ch) { contents[r*width + c] = ch; *this; } void Screen::some_member() const { ++access_ctr; } inline Screen &Screen::move(pos r, pos c) { pos row = r *width; cursor = row + c; return *this; } char Screen::get(pos r, pos c) const { pos row = r * width; return contents[row + c]; } //--------------------------------- class Window_mgr{ private: using VECTOR = std::vector<Screen> ; // A Window Manager has has one standard sized blank Screen VECTOR screens = VECTOR{ {Screen(24, 80, ' '), Screen(32, 56, 'r') }}; // <----------------- }; //--------------------------------- int main() { Screen scr{13, 33, 'c'}; std::cout << scr.get(3, 1); Window_mgr win{}; return 0; }