Path: utzoo!attcan!uunet!wuarchive!zaphod.mps.ohio-state.edu!sol.ctr.columbia.edu!ira.uka.de!fuchs From: fuchs@it.uka.de (Harald Fuchs) Newsgroups: comp.lang.c++ Subject: operator& Message-ID: Date: 31 Oct 90 23:51:22 GMT Sender: news@ira.uka.de (USENET News System) Organization: University of Karlsruhe, FRG Lines: 49 I tried to define a counted pointer class "Xptr" to another class "X" and ran into a problem with the address-of "operator&". I redefined this operator to return a counted rather than a normal pointer, but for Xptr::operator-> () I need the real address of my X object (i.e. an X* rather than an Xptr). Is there a way to access the "normal" address-of operator even if it is redefined for a user-defined type? // Xptr.h #include "X.h" class Xptr { class Xrep { friend class Xptr; friend Xptr operator& (const X&); unsigned short ref; X obj; Xrep (const X& x): ref (0), obj (x) {} void deref () { if (!--ref) delete this; } }; Xrep* p; static Xrep*const nil; public: Xptr (Xrep* x = 0): p (x ? x : nil) { p->ref++; } Xptr (const Xptr& x): p (x.p) { p->ref++; } ~Xptr () { p->deref (); } Xptr& operator= (const Xptr& x) { x.p->ref++; p->deref (); p = x.p; return *this; } operator char () const { return p != nil; } // Usage: if (cp) ... X& operator* () const { return p->obj; } //X* operator-> () const { return &p->obj; } // This won't work X* operator-> () const { return (X*) ((char*) p + sizeof p->ref); } // Ugly hack: don't call operator& for an X }; inline Xptr operator& (const X& x) { return new Xptr::Xrep (x); } // Usage: X obj; Xptr p = &obj; // Xptr.C #include "Xptr.h" static unsigned short null = 1; // Never deallocate Xptr::Xrep*const Xptr::nil = (Xptr::Xrep*) &null; -- Harald Fuchs ... *gulp*