From: Sean Middleditch Date: 2002-03-30T15:56:18+09:00 Subject: Re: Creating an instance of a class without calling initialize() On Sat, 2002-03-30 at 01:44, Michael Brailsford wrote: > > Is there a way to do that in pure Ruby, or, if not, to > > write a C extension that allows me to do it by interacting > > with the VM somehow? > > I don't know you would want that, but you can work around it by giving a > blank implementation to the initialize method, then when it is called > nothing will be done to set the initial state of the object. Unless you > are dead set against calling an intialize method that should succeed in > not setting up any initial state. Are there any OO languages that allow > you to create an object with calling its constructor? I know you can't > in java, nor c++ so far as I know. Seems to me to be a fundamental > tenet of OO programming. Perhaps a proceudral language would be more > suited to what you want. Sure you can in C++. Use new to allocate the correct number of bytes for the instance, manually set the member values to the correct function pointers, and there you go. Granted, with most C++ compilers, you're going to have to pull some casting tricks to be able to set the function pointers correctly... ~,^ But you're right, why would you want to do that? It'd be better to, if you don't want the object to "initialize" on creation, not give it an initialization method, and instead give it a "start" method or some such to initialize. This would be useful where your initialization requires various parameters, and you are buffering the creation for objects for speed; also, if your object creates resource handles to sockets/files/etc, and you want to hold off on that until necessary. Those kinds of activities don't really belong in initializers either, however, following good programming practice (which, personally, I believe quite often is not in line with standard OO practice). Sean Etc. > > -Michael > > -- > ### Hiroshima 45, Chernobyl 86, Windows 95 ###