Path: utzoo!attcan!utgpu!jarvis.csri.toronto.edu!mailrus!ames!sun-barr!rutgers!att!cbnewsl!dog From: dog@cbnewsl.ATT.COM (edward.n.schiebel) Newsgroups: comp.lang.c++ Subject: Re: Objectifying incoming messages? Message-ID: <1567@cbnewsl.ATT.COM> Date: 22 Aug 89 11:35:17 GMT References: <828@otc.otca.oz> Organization: AT&T Bell Laboratories Lines: 44 From article <828@otc.otca.oz>, by gregw@otc.otca.oz.au (Greg Wilkins): [stuff deleted] > However I think you have invented an extra problem for yourself. > If you cannot completely construct an object until a message is sent and > a reply recieved, then > I see the the solution is to construct a base class, this bass class > then sends the message to find out > which particular specialization it is and coerces itself to a derived > class when it recieves the reply. > You cannot do this. The memory allocated at the time of the call to the constructor is for a base class. By coercing it to a derived class you 1. are probable going to walk of the end of your memory 2. are requiring a base class to know of its derived classes, something which you may want to avoid. One possible way around this is to define a base class which defines the interface to your hierarchy of objects which manages only a pointer to another base class, the real base class of your hierarchy. The interface-base's constructor can figure out what type of object is needed, allocate/construct it, and set its member pointer-to-base-class to the new object. It costs you a level of indirection, but it works. Another win with this scheme, is you can change the apparent type of the object at run-type by deleting the current poniter and re-creating a new different one from the hierrchy. i.e.: class interface_base { public: // interface functions void interface_function1() {ptr->interface_function1();} private: abstract_base* ptr; // points to one of many possibilities // in the hierarchy }; Ed Schiebel AT&T Bell Laboratories ...att!vilya!dog ps. thanks to jonathan shopiro for this idea a couple years ago!