Path: utzoo!attcan!uunet!aplcen!samsung!usc!elroy.jpl.nasa.gov!ames!vsi1!octopus!sjsumcs!horstman From: horstman@sjsumcs.sjsu.edu (Cay Horstmann) Newsgroups: comp.lang.c++ Subject: Re: calling (con|de)structors explicitly Keywords: c++1.2, constructor Message-ID: <1990Feb11.164521.18287@sjsumcs.sjsu.edu> Date: 11 Feb 90 16:45:21 GMT References: <1088@tuvie> Reply-To: horstman@sjsumcs.SJSU.EDU (Cay Horstmann) Organization: San Jose State University Lines: 22 In article <1088@tuvie> inst182@tuvie (Inst.f.Techn.Informatik) writes: > >I have a problem: I want to dynamically create an array of >a class as a member of another class. Now I cannot use the >new operator, because the constructors of this class all >have at least one argument. Thus, I'd like to allocate the >memory with malloc and call the constructor explicitly >for each element. Can I do anything to make >this legal: > > In C++ 2.0, you can launch a constructor on a pre-allocated area, but it is not a pretty sight. You say "new (p) X( arg )" to run X::X( arg ) on the area of memory starting at p. See Lippmann for the details. I suppose you are doing this because you don't own X--otherwise I think it would make far more sense to add a X::X() and an explicit function to do the initialization, say X::init( arg ). Cay