delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/2000/02/29/13:53:57

From: Hans-Bernhard Broeker <broeker AT acp3bf DOT physik DOT rwth-aachen DOT de>
Newsgroups: comp.os.msdos.djgpp
Subject: Re: use NULL in c++
Date: 29 Feb 2000 17:48:19 GMT
Organization: Aachen University of Technology (RWTH)
Lines: 29
Message-ID: <89h0p3$m5d$1@nets3.rz.RWTH-Aachen.DE>
References: <38bbf095$0$85200 AT SSP1NO17 DOT highway DOT telekom DOT at>
NNTP-Posting-Host: acp3bf.physik.rwth-aachen.de
X-Trace: nets3.rz.RWTH-Aachen.DE 951846499 22701 137.226.32.75 (29 Feb 2000 17:48:19 GMT)
X-Complaints-To: abuse AT rwth-aachen DOT de
NNTP-Posting-Date: 29 Feb 2000 17:48:19 GMT
User-Agent: tin/1.4-19991113 ("No Labels") (UNIX) (Linux/2.0.0 (i586))
Originator: broeker@
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp
Reply-To: djgpp AT delorie DOT com

Florian X <dos DOT fire AT aon DOT at> wrote:

> If I use NULL in c++, the compiler prints me this error:

You forgot to show us *how* you use NULL, in your program. That's an
important detail you're hiding from us.

> dc.cc:167: ANSI C++ forbids implicit conversion from `void *' in argument
> passing

The error message says it all, I think. Unlike C, C++ does not allow
to use a 'void *' expression (like NULL) where a pointer to some
specific type is requested. In C, you can do that, as the 'void *'
will automatically be casted to the right type. That's the 'implicit
conversion' the error message speaks of. In C++, you have to write
down the cast, in the source code. The typical example:

	some_type *p = malloc(number*sizeof(some_type));

does not work in C++, although it is perfectly valid C. In C++, you
have to write the following, instead:

	some_type *p = (some_type*) malloc(number*sizeof(some_type));

(Or use one of the specialized cast operators offered by C++, like
'static_cast')
-- 
Hans-Bernhard Broeker (broeker AT physik DOT rwth-aachen DOT de)
Even if all the snow were burnt, ashes would remain.

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019