smart pointers -- C++ Primer

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
 
A

Andrew Koenig

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top