Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!zaphod.mps.ohio-state.edu!samsung!olivea!uunet!bywater!arnor!watson!blinn.watson.ibm.com!mittle From: mittle@blinn.watson.ibm.com (Josh Mittleman) Newsgroups: comp.lang.c++ Subject: Re: Want pointers on intelligent pointers Message-ID: <1991Mar18.220013.18185@watson.ibm.com> Date: 18 Mar 91 22:00:13 GMT References: <838@antares.Concordia.CA> Sender: @watson.ibm.com Reply-To: mittle@ibm.com Organization: IBM T. J. Watson Research Lines: 66 In article <838@antares.Concordia.CA>, marcap@antares.concordia.ca ( MARC ANDREW PAWLOWSKY ) writes: I think you're may be going about the reference count backward. The reference count is controlled by the smart pointer, not the object. Consider this approach. class Object { friend class ObjectP; protected: int refcount; Object() { refcount = 1; } public: ~Object(); }; class ObjectP { protected: Object* myOb; public: ObjectP() { myOb = new Object(); } ObjectP(const ObjectP& ObP); { myOb = ObP.myOb; myOb->refcount++; } ~ObjectP(); { if (myOb->refcount == 1) delete myOb; else myOb->refcount--; } }; ObjectP::ObjectP() { myOb = new Object(); } When you need an Object, you actually declare an ObjectP. > In my creation routines can I refer the the object being created? > I have a class Box which has a global variable called "default_box". > I also have a function "operator =" that will copy a box into > this. can I do something like > > Box::Box(void) { > this = default_box; // does not work with TC++ 1.01 > return this; // return not allowed > } The first line is an anachronism (see ARM, p.406, Assignment to this). The type of this in the class Box is Box *const (ARM, p.177). The C++ approach would be to include a field-by-field assignment in Box, like this: Box::Box() { width = 50; height = 20; } This code does refer to the object being constructed, by referencing its members. Constructors do not return anything in C++. On the other hand, the operator new does. This is correct: Box *B = new Box; =========================================================================== Josh Mittleman (mittle@ibm.com or joshua@paul.rutgers.edu) J2-C28 T.J. Watson Research Center, PO Box 704, Yorktown Heights, NY 10598