TIP: Click on subject to list as thread! ANSI
echo: c_plusplus
to: MATHIEU BOUCHARD
from: RON BASS
date: 1998-01-30 00:09:00
subject: Re: converting char to string

You wrote:
 MB> NULL is defined as being of type void *.
This may be true in C, but in C++, if NULL is defined at all it is defined to 
be the constant integer value of 0.  This is discussed at length in Scott 
Meyer's "Effective C++" (get the second edition).
This is because the stronger typing rules in C++ means that a pointer to a 
real class type cannot be assigned a value defined as a void pointer type.  
In fact, the only assignment between different pointer types that is valid is 
to assign a pointer for a derived class type to a pointer to a base class 
type.  Any other pointer assignemnt or pointer comparison is illegal under 
the proposed C++ standard and will be flagged as an error by any reasonably 
up to date compiler.
The proposed standard explicitly allows a pointer to be assigned the constant 
integer value of 0 to create a NULL pointer and to be compared to the 
constant integer value 0 as a check for a NULL pointer.
Hence if we look at the following code...
  class A
  {
   ...// some class definition
  };
  class B : public A
  {
   ... // some derived class definition
  };
and then in the main body of the code we have
  A foo; // foo is an A on the stack
  B bar; // bar is a B on the stack
  A* pF = &foo; // valid (in a trivial way)
  A* pB = &bar; // valid since a B "is-a" A
  A* pANULL = 0; // valid since assingment to 0 is allowed
  void* pVNULL = 0; // also valid
  pANULL = pVNULL; // error because A is not
                   // derived from a "void object"
  pANULL = (void*) 0; // error
 MB> NULL is reserved for pointers; i often use 0 instead, though. I'm not
 MB> sure whether it is actually desirable that i recommend using 0,
 MB> though... :-)
Under C++, the constant integer 0 is the only valid way to represent a NULL 
pointer that can be used with any type of pointer.
    -Ron  (ron-bass@ti.com)
--- EZPoint V2.2
---------------
* Origin: There's a point here, somewhere... (1:128/13.3)

SOURCE: echomail via exec-pc

Email questions or comments to sysop@ipingthereforeiam.com
All parts of this website painstakingly hand-crafted in the U.S.A.!
IPTIA BBS/MUD/Terminal/Game Server List, © 2025 IPTIA Consulting™.