Question about text -- Effective c++

P

pauldepstein

On page 82 of Effective C++, an example of non-compilable code is given
as follows:

std::tr1::shared_ptr<Investment>
pInv(0, getRidOfInvestment);

I understand this part -- 0 is not a valid parameter for pInv because 0
is an int and not a pointer.

Here is the text's intended correction:

std::tr1::shared_ptr<Investment>
pInv( static_cast<Investment*>(0),
getRidOfInvestment);

I understand why this works. Here's what I don't understand --
Wouldn't a simpler correction be to use pInv(NULL, getRidOfInvestment);
If the problem is that 0 is not a pointer, why not use the intended
NULL pointer. Isn't NULL a pointer? My correction seems clearer, and
this leads me to suspect that I might be missing something.

Thank you.

Paul Epstein
 
A

Alf P. Steinbach

* (e-mail address removed):
On page 82 of Effective C++, an example of non-compilable code is given
as follows:

std::tr1::shared_ptr<Investment>
pInv(0, getRidOfInvestment);

I understand this part -- 0 is not a valid parameter for pInv because 0
is an int and not a pointer.

Here is the text's intended correction:

std::tr1::shared_ptr<Investment>
pInv( static_cast<Investment*>(0),
getRidOfInvestment);

I understand why this works. Here's what I don't understand --
Wouldn't a simpler correction be to use pInv(NULL, getRidOfInvestment);
If the problem is that 0 is not a pointer, why not use the intended
NULL pointer. Isn't NULL a pointer?

No, not in C++.

NULL is (required to be) a zero integer constant.

A zero integer constant is a nullpointer in a context where a pointer
value is expected.
 
C

Christof Warlich

Isn't NULL a pointer? My correction seems clearer, and
this leads me to suspect that I might be missing something.

Hi Paul,

NULL is just a #define that is at best defined as

#define NULL ((void *) 0)

Thus, if you use NULL, you'll still get a compiler error as
the pointer type (void *) does not match the type being requested.

Hope this answers your question.

Cheers,

Christof
 
A

Alf P. Steinbach

* Christof Warlich:
Hi Paul,

NULL is just a #define that is at best defined as

#define NULL ((void *) 0)

Sorry, that's incorrect. NULL is never defined as a void* in a
conforming C++ implementation.
 
F

Frederick Gotham

Alf P. Steinbach posted:
* Christof Warlich:

Sorry, that's incorrect. NULL is never defined as a void* in a
conforming C++ implementation.


Indeed, if it were to be defined as (void*)0 in C++, then the following would
result in a type mismatch:

int *p = NULL;

, because there's no implicit conversion from void* to int*.
 
M

Marcus Kwok

On page 82 of Effective C++, an example of non-compilable code is given
as follows:

std::tr1::shared_ptr<Investment>
pInv(0, getRidOfInvestment);

I understand this part -- 0 is not a valid parameter for pInv because 0
is an int and not a pointer.

Here is the text's intended correction:

std::tr1::shared_ptr<Investment>
pInv( static_cast<Investment*>(0),
getRidOfInvestment);

I understand why this works. Here's what I don't understand --
Wouldn't a simpler correction be to use pInv(NULL, getRidOfInvestment);
If the problem is that 0 is not a pointer, why not use the intended
NULL pointer. Isn't NULL a pointer? My correction seems clearer, and
this leads me to suspect that I might be missing something.

Others have answered your question, but FYI there is a proposal to add a
special null pointer type:

http://www.open-std.org/JTC1/SC22/WG21/docs/papers/2004/n1601.pdf
 

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,011
Latest member
AjaUqq1950

Latest Threads

Top