2

I have read the book C++ Primer. In section 7.3.1: there is a constructor of Screen class:

class Screen {
public:
    typedef std::string::size_type pos;
    Screen() = default; 
    Screen(pos ht, pos wd, char c): height(ht), width(wd),
                                     contents(ht * wd, c) { }
    char get() const { return contents[cursor]; } 
    inline char get(pos ht, pos wd) const;
    Screen &move(pos r, pos c);
private:
    pos cursor = 0;
    pos height = 0, width = 0;
    std::string contents;
};

And in the overloaded constructor function:

Screen(pos ht, pos wd, char c): height(ht), width(wd),
                                 contents(ht * wd, c) { }

What is the initial value of contents(ht * wd, c) and how it works?
In the section 7.1.4, there states:

The constructor initializer is a list of member names, each of which is followed by that member’s initial value in parentheses (or inside curly braces).

And I have known that string has a way string s(n, 'c') to initialize a string such as string s(10, 'c').
But how it works to utilize the string constructor in the constructor member initialization?
Thanks in advance.

zhenguoli
  • 2,268
  • 1
  • 14
  • 31
  • It's initializing `contents` by calling the constructor as `std::string::string(ht * wd, c)`. – songyuanyao Jul 26 '17 at 05:13
  • @songyuanyao, but what about other member initialization? If it is a built-in type, how it works? – zhenguoli Jul 26 '17 at 05:14
  • Just as the quotes said, `height` is initialized with the initial value `ht`, `width` is initialized with the initial value `wd`. – songyuanyao Jul 26 '17 at 05:17
  • What I want to know is that why it will call the `string` constructor? According to my limited knowledge, I can't image that it will call the `string` constructor automatically. And in any other language like Javascript, there is no way to call function in constructor like this. @songyuanyao. – zhenguoli Jul 26 '17 at 05:21
  • That's the way you initialize an object with class type in C++, almost always the constructor of the class type will be invoked, member initializer list is just one of these ways. BTW: If you don't specify it in the member initializer list, `contents` will be initialized by the default constructor of `std::string`. – songyuanyao Jul 26 '17 at 05:29
  • @songyuanyao. Thank you. But there is no introduction about this in the book C++ Primer. So I am confused. Is there any material about this? – zhenguoli Jul 26 '17 at 05:33
  • I think these things must be explained somewhere in the book. You can refer to [here](http://en.cppreference.com/w/cpp/language/initializer_list) for more infos about member initializer list. – songyuanyao Jul 26 '17 at 05:36
  • @songyuanyao. Thank you very much. – zhenguoli Jul 26 '17 at 05:37
  • i found this sentence after 4-5 pages after the code(shown above) " When we initialize a member of class type, we are supplying arguments to a constructor of that member’s type" in the book – kapil Jan 17 '20 at 16:38

1 Answers1

1

I also encounter this problem when reading about this. As indicated by songyuanyao, my guessing is that when we use parenthesis or curly braces in the constructor list, the corresponding constructor of each class data member is automatically called by the compiler to initialize the function parameters. For example, in

Screen(pos ht, pos wd, char c): height(ht), width(wd), contents(ht * wd, c) { }

The function parameter height, of type int, is initialized with int(ht);
The function parameter width, of type int, is initialized with int(wd);
The function parameter contents, of type std::string, is initialized with std::string(ht * wd, c);

Feel free to tell me if my answer is not right.

PS: Thanks to @M.M for pointing out my mistake.

yyFred
  • 775
  • 9
  • 13
  • Initialization is different to assignment. The arguments go to the constructor of the class member. It doesn't construct a temporary as in your example and then assign that to your default-constructed class member. – M.M Jul 11 '19 at 04:42