Newsgroups: comp.lang.c++ Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!batcomputer!theory.tn.cornell.edu!lijewski From: lijewski@theory.tn.cornell.edu (Mike Lijewski) Subject: Re: Constructor and class initialization w/ arrays Message-ID: <1991Mar29.013805.19560@batcomputer.tn.cornell.edu> Keywords: How do you do it? Sender: news@batcomputer.tn.cornell.edu Nntp-Posting-Host: theory.tn.cornell.edu Organization: Cornell National Supercomputer Facility References: <3118@beguine.UUCP> <642@taumet.com> Date: Fri, 29 Mar 1991 01:38:05 GMT In article <642@taumet.com> steve@taumet.com (Stephen Clamage) writes: >Brian.Chan@samba.acs.unc.edu (Brian Chan) writes: > >>Say I have the following declarations: >>Class A: { int a; int b; } >>Class B: public A { ... } >>And constructors: >>Class A::A(int x, int y) { } >>Class B::B(int z, int x, int y) :A(x, y) { ... } >> ... >>But if I want to declare a table (array) of Bs, say , 2, how do I do it? > >You cannot declare an array of class objects using other than the default >(no parameters) constructor. There is no syntax to support it. You can >declare a constructor with all default parameters standing in for the >default constructor, and that one will be used. That's the best you can do. >(This last is a language change, and works with recent C++ compilers.) You certainly can declare arrays of class objects using other than the default constructor. The following declares an array of four objects of class B: class B barray[] = { B(1,2,3), B(4,5,6), B(7,8,9), B(10,11,12) }; >-- > >Steve Clamage, TauMetric Corp, steve@taumet.com -- Mike Lijewski (H)607/272-0238 (W)607/254-8686 Cornell National Supercomputer Facility ARPA: mjlx@eagle.cnsf.cornell.edu BITNET: mjlx@cornellf.bitnet SMAIL: 25 Renwick Heights Road, Ithaca, NY 14850