Constructor initializer list: code from the C++ Primer, chapter 16
- by Alexandros Gezerlis
Toward the end of Chapter 16 of the "C++ Primer" I encountered the following code (I've removed 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() { } line.
For the sake of completeness, let me also quote the parts of the Handle class template that I think are relevant to my question (I think I don't 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 have expected something like either:
a) Sales_item(): h(0) { } which is a convention the authors have used repeatedly in earlier chapters, or
b) Handle<Item_base>() if the intention was to invoke the default constructor of the Handle class.
Instead, what the book has is Sales_item(): h() { }. My gut reaction is that this is a typo, since h() looks suspiciously similar to a function declaration. On the other hand, I just tried compiling under g++ and running the example code that uses this class and it seems to be working correctly. Any thoughts?