Constructor Initialization List: C ++ Primer Code, Chapter 16

At the end of chapter 16 of the “C ++ Primer”, I came across the following code (I deleted a bunch of lines):

class Sales_item { public: // default constructor: unbound handle Sales_item(): h() { } private: Handle<Item_base> h; // use-counted handle }; 

My problem is with the Sales_item(): h() { } .

For completeness, let me also indicate the parts of the Handle class template that, in my opinion, are relevant to my question (I think I do not need to show the Item_base class):

 template <class T> class Handle { public: // unbound handle Handle(T *p = 0): ptr(p), use(new size_t(1)) { } private: T* ptr; // shared object size_t *use; // count of how many Handles point to *ptr }; 

I would expect something like:

a) Sales_item(): h(0) { } , which is a convention that authors have repeatedly used in previous chapters, or

b) Handle<Item_base>() , if the intention was to invoke the default constructor of the Handle class.

Instead of what is in the book, Sales_item(): h() { } . My gut reaction is that it is a typo, since h () looks suspiciously like a function declaration. On the other hand, I just tried to compile under g ++ and run the sample code that uses this class, and it seems to work correctly. Any thoughts?

EDIT: All the good answers, thanks! In the intermediate 30 minutes, I traced the corresponding quote from chapter 12 of the same book: "When we initialize a member of a class type, we specify the arguments that should be passed to one of the constructors of this element type. We can use any of the constructors of this type." And, as you all pointed out, in this case we pass null arguments.

+4
source share
3 answers

What do you have with Sales_item(): h() { } is a constructor with initialization of a data member.

I would expect something like:

a) Sales_item(): h(0) { } , which is a convention that authors have repeatedly used in previous chapters, or

This is not necessary because the Handle<Item_base>() constructor can be called without an argument. (Its one argument has a default value, so it can be omitted.)

b) Handle<Item_base>() , if the intention was to invoke the default constructor of the Handle class.

This is just the wrong syntax. This syntax is used for base classes, and there it is fine, since any class can inherit only once from Handle<Item_base>() . However, it can have many data elements of this type, so its name is used to initialize the desired data element, not its type.


This is a very good book from which you learn, BTW. After you go through it, you can look at The Definitive C ++ Guide and List for better input.

+6
source

The code is completely correct - the h () function uses the default constructor to build h. In this case, this is also optional, since the default constructor will be used if you do not give explicit initialization. A default constructor is any constructor that can be called without arguments, which the handle class has because of its default argument value.

+2
source

h is the name of the Sales_item member Sales_item , and its Handle class has a constructor with a default parameter, so h() builds the element correctly. This is equivalent to h(0) .

+1
source

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


All Articles