Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!tut.cis.ohio-state.edu!rutgers!ucsd!sdcsvax!ucsdhub!hp-sdd!hplabs!hpda!hpwala!hpwale!charlien From: charlien@hpwale.HP.COM (Charlie Nuzzolo) Newsgroups: comp.lang.c++ Subject: Re: using references vrs. pointers Message-ID: <920005@hpwale.HP.COM> Date: 27 Jan 89 16:31:57 GMT References: <417@vilya.UUCP> Organization: HP Waltham Division Lines: 42 >Thus, I would like to get away from memory pointers in my C++ programs and >make greater use of references. However, I seem to have no way of initializing >a reference to refer to nothing, or testing if it refers to nothing (akin >to a pointer to NULL). How about using macros: #define NULL_REF(type) *(type*)NULL #define IS_NULL_REF(arg) ((void*)&arg == NULL) Then you can do things like: MyClass& obj = NULL_REF(MyClass); and: MyTypedef& myType = NULL_REF(MyTypedef); You can also pass null references for arguments: void myFunction(MyType& t = NULL_REF(MyType)) { if (IS_NULL_REF(t)) printf("default\n"); else printf("not null default\n"); } You can do similar things with class references too but be sure to use 'const MyClass&' to avoid constructor problems. Charlie Nuzzolo Hewlett Packard (617) 890 6300 charlien@hpwarg.HP.COM