P
pauldepstein
I am reading Section 13.5 Managing Pointer Members of C++ Primer by
Lippman, Lajoie and Moo
Section 13.5.2 Defining Valuelike Classes
Here is some of code to illustrate the value semantics technique for
managing pointers.
// Each time we copy a HasPtr object, we make a new copy of
// the underlying int object to which ptr points.
class HasPtr {
public: HasPtr(const int &p, int i): ptr(new int(p)), val(i) {}
//
//
private int* ptr;
int val;}
When handling integers, we normally make copies instead of call by
reference.
So I would have expected const int p instead of const int &p
Suppose the text said const int p
Then, in my opinion, a pointer would have been dynamically allocated
to point to an object with value p.
What's wrong with that?
Paul Epstein
Lippman, Lajoie and Moo
Section 13.5.2 Defining Valuelike Classes
Here is some of code to illustrate the value semantics technique for
managing pointers.
// Each time we copy a HasPtr object, we make a new copy of
// the underlying int object to which ptr points.
class HasPtr {
public: HasPtr(const int &p, int i): ptr(new int(p)), val(i) {}
//
//
private int* ptr;
int val;}
When handling integers, we normally make copies instead of call by
reference.
So I would have expected const int p instead of const int &p
Suppose the text said const int p
Then, in my opinion, a pointer would have been dynamically allocated
to point to an object with value p.
What's wrong with that?
Paul Epstein