4

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; } 

1 Answer 1

2

In C++, it's perfectly valid to construct an instance of a class inline with a method call (essentially creating an anonymous instance of the class). So the syntax

VECTOR{ {Screen(24, 80, ' '), Screen(32, 56, 'r') }} 

Is initializing a VECTOR with two instances of Screen. It also binds their lifecycle to that of the VECTOR without having to make a copy; they can be move'd safely since their scope is just the constructor call.

Sign up to request clarification or add additional context in comments.

1 Comment

Yeah. In C++ that syntax creates a anonymous object on the spot. If we want to store it then need to give a object name otherwise not. In terms of vector that anonymous object becoming an element of that vector.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.