[comp.lang.c++] Why doesn't a void* & parameter get modified in C++ 2.0?

larry@wizard.dsg.tandem.com (Larry Wear) (04/13/90)

The following program works correctly using AT&T's cfront 1.2,
however it does not work correctly using Sun's cfront 2.0.  The
NullIt method in the class go does not modify the pointer value
of name.  I would like to know why.  Thanks.         

#include <malloc.h>
#include <stream.h>

#define NULL 0
typedef void* ANY;

class bigFun
{
 public:

  void NullIt( ANY & pObject )
    {
      pObject = NULL;
    }
};

void main()
{
  bigFun go;
  char * name  = malloc(5);

  go.NullIt( (ANY)name );
  if ( name == NULL ) cout << "Success\n";
  else cout << "Failed\n";
}

--
Larry Wear                wizard!larry@Tandem.COM
Tandem Computers

c60c-2ca@e260-2d.berkeley.edu (Andrew Choi) (04/14/90)

In article <1208@wizard.dsg.tandem.com> larry@wizard.dsg.tandem.com (Larry Wear) writes:
>The following program works correctly using AT&T's cfront 1.2,
>however it does not work correctly using Sun's cfront 2.0.  The
>NullIt method in the class go does not modify the pointer value
>of name.  I would like to know why.  Thanks.         
>
>#include <malloc.h>
>#include <stream.h>
>
>#define NULL 0
>typedef void* ANY;
>
>class bigFun
>{
> public:
>
>  void NullIt( ANY & pObject )
>    {
>      pObject = NULL;
>    }
>};
>
>void main()
>{
>  bigFun go;
>  char * name  = malloc(5);
>
>  go.NullIt( (ANY)name );
>  if ( name == NULL ) cout << "Success\n";
>  else cout << "Failed\n";
>}
>
>--
>Larry Wear                wizard!larry@Tandem.COM
>Tandem Computers


The reason is that a temporary variable will be created if the type of
reference variable does not match exactly the type of its assigned value or
if the assigned value is a literal.  The cast really does not
affect anything because it merely informs the compiler which conversion
is to be used.

----------

Andrew Choi
Internet Address:  c60c-2ca
Standard Disclaimer