Path: utzoo!attcan!uunet!mcsun!ukc!edcastle!ecsv38 From: ecsv38@castle.ed.ac.uk (S Manoharan) Newsgroups: comp.lang.c++ Subject: `new'ing an object that has a constructor Message-ID: <5137@castle.ed.ac.uk> Date: 12 Jul 90 12:00:33 GMT Reply-To: ecsv38@castle.ed.ac.uk (S Manoharan) Organization: Dept of Computer Science, University of Edinburgh Lines: 37 Well, the subject line says it all. How do I `new' an object that's got a constructor? I am interested in `new'ing an array of such objects. Consider the following code: class A { private: int sz; public: A(int n) { sz = n; } void foo() { cout << form("%d\n", sz); } }; int main() { A *p1 = new A(3); A *p2 = new A[10]; // <--------- Problem's here. p1->foo(); return 0; } I am using g++ 1.37.1. It complains "too few arguments for constructor `A'". Fine. Now if I change the line to: A *p2 = new A(3)[10]; // <--------- Problem's here. I get a complaint "parse error before `['". Where do I go wrong? Or, how do I get around this? Lotsa thanks in advance. Manoharan. Ps - I tried it with cfront 1.2.1 [Sorry, that's all we've got here] and it gives a compile time Memory fault in both cases. [ 701 Memory fault in the 1st case and 695 Mem Fault in the 2nd ]