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:
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:
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.