Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!tut.cis.ohio-state.edu!cs.utexas.edu!uunet!mcvax!kth!sunic!Urd!newsuser From: newsuser@LTH.Se (LTH network news server) Newsgroups: comp.lang.c++ Subject: Smart pointers in C++ 2.0 Message-ID: <1989May3.162803.28414@LTH.Se> Date: 3 May 89 15:28:02 GMT Reply-To: dag@Control.LTH.Se (Dag Bruck) Organization: Dept. of Automatic Control, Lund Inst. of Technology, Sweden Lines: 58 Now that C++ 2.0 is about to make it, it seems natural to look at some new possibilities. One new feature is overloading of operator -> (), which can be used for implementing ``smart pointers'' [1]. Using InterViews [2], I often create Painter objects. For the sake of this discussion, assume I want to create a smart pointer to Painter which checks for nil pointers. Today, my code looks like this: class Painter; ... Painter* output = new Painter; output->Rect(0, 0, 1, 1); With C++ 2.0 I can create a smart pointer type, like this: class Painter; class PainterPtr { private: Painter* p; public: PainterPtr() { p = 0; } PainterPtr& operator = (Painter* q) { p = q; return *this; } Painter* operator -> () { if (p == 0) error(); else return p; } }; ... PainterPtr output = new Painter; output->Rect(0, 0, 1, 1); This is great, I think. I my crystal ball, I can see a number of applications of smart pointers (including less trivial uses). The object-oriented jargon is probably (limited) ``delegation.'' Unfortunately, all my existing code, including 30000 lines of InterViews must be changed so ``Painter*'' is replaced by ``PainterPtr.'' My question is now: is there a way to get the same ``smart'' functionality I outlined above, without having to change all my existing programs? Any other comments are welcome, of course (Is it really useful? Can it be done in some other way? Why don't you use LISP?). [1] Bjarne Stroustrup: ``The Evolution of C++: 1985 to 1987,'' Proc. USENIX C++ Workshop, Santa FE, NM, 1987. [2] Linton, Vlissides and Calder: ``Composing User Interfaces with InterViews,'' IEEE Computer, February 1989. Dag Michael Bruck -- Department of Automatic Control Internet: dag@control.lth.se Lund Institute of Technology P. O. Box 118 Phone: +46 46-108779 S-221 00 Lund, SWEDEN Fax: +46 46-138118