Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!wuarchive!sdd.hp.com!spool2.mu.edu!uunet!softrue!kearns From: kearns@softrue.UUCP (Steven Kearns) Newsgroups: comp.lang.c++ Subject: Re: Smarter pointers - another solution Message-ID: <3.UUL1.3#8618@softrue.UUCP> Date: 24 Jan 91 15:01:01 GMT References: <3454@lupine.NCD.COM> Organization: Software Truth Lines: 68 I am sure this must have been mentioned before, but I cannot recollect the discussion, if there is one... What is wrong with allowing one to define "class T* {.....}" which defines a new class that replaces the usual type (T *)? Since this class is sort of implicitly named "T*", in the following it will be convenient to use an explicit name: "Tpointer". But this is just an artifact of our discussion, and such a name never appears in the program, or compiler. Class Tpointer is a typical class, following the same rules as any class. The only difference is that anywhere in a program T* is referred to, class Tpointer is the type actually referred to. For example, the definition T * tp; Would really be Tpointer tp; Similarly, int joe; T*tp = new T(joe); is really int joe; Tpointer tp = new T(joe); which really calls the tp(T*) constructor and then the tp(tp&) copy constructor. This, by definition, makes it impossible for a programmer to get an actual T* value, because anytime the programmer specifies such a value, the class Tpointer will be the one actually referred to. By symmetry, one would expect that we could do the same thing for class T& {...}. In reality, almost all applications will want class T& to behave in all circumstances like a "dereferenced (T * const)" would behave. For example, T myt; T& tr = myt; tr.t_operation(3); is usually intended to mean: T myt; T* const tr = &myt; (*tr).t_operation(3); This can be made explicit, perhaps, so that class T& would not have to be declared after class *T was. One potential criticism is that the implementors of class T* might want to use the original "pointer to T" type. But I believe that C++ already has a facility for this. To declare foo as a true pointer to T one could write "T ::operator* (foo)", and to dereference foo one could write (foo.::operator*()). Admittedly this is gross, but you cannot have your cake and eat it too, and I think that it is much better to make the class programmer type some more, than to have the class user think some more. Another potential criticism is that C++ should not redefine the meaning of built in operators. Well, where is that written in stone? In the case of pointers there are extremely good reasons to do so. -steve ******************************************************** * Steven Kearns ....uunet!softrue!kearns * * Software Truth softrue!kearns@uunet.uu.net * ********************************************************