Path: utzoo!attcan!utgpu!jarvis.csri.toronto.edu!mailrus!uunet!mcsun!hp4nl!dutrun!ben From: ben@duttnph.tudelft.nl (Ben Verwer) Newsgroups: comp.lang.c++ Subject: Variable sized objects Message-ID: <1020@dutrun.UUCP> Date: 4 Dec 89 16:29:10 GMT Sender: tnphnws@dutrun.UUCP Reply-To: ben@duttnph.tudelft.nl (Ben Verwer) Organization: Delft University of Technology, The Netherlands Lines: 46 How do you implement variable sized objects in 2.0 Consider the example of a stack from Stroustrup, page 165: class char_stack { int size; char *top; char s[1]; public: char_stack(int sz); void push(char c) // etc... char pop() // etc... }; char_stack::char_stack(int sz) { this = (char_stack*)new char[sizeof(char_stack)+sz-1]; size = sz; top = s; } You can allocate a new stack by the statement: char_stack* mystack = new char_stack(1000); In 2.0 operator new has be be used instead of assignment to this. But both operator new and the constructor need to know the size of the object. Should it be something ugly like (probably not, it does not even compile, though I don't understand why not, "too few arguments for method `operator new'"): char_stack* mystack = new(1000) char_stack(1000); // tell the same thing twice With: void* char_stack::operator new(size_t SizeWithOneChar, int sz) { return (char_stack*) new char[SizeWithOneChar+sz-1]; } char_stack::char_stack(int sz) { size = sz; top = s; } A user experiencing an upgrade in a language! Help me can onesome? ----------------------------------------------------------------------------- Ben Verwer Lorentzweg 1 Pattern Recognition Group 2628 CJ Delft Faculty of Applied Physics The Netherlands Delft University of Technology +31(15)783247