[comp.lang.c++] Constructor and class initialization w/ arrays

Brian.Chan@samba.acs.unc.edu (Brian Chan) (03/27/91)

Can someone help me with a beginner's question?
 
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)
{
...
}
 
If I declare B b1(1,1,1), b2(2,2,2), no problem.
 
But if I want to declare a table (array) of Bs, say , 2, how do I do it?
 
I tried the following:
 
B b[] = { {1, 1, 1}, {2, 2, 2}};  // won't work
B b[2];                 // won't work obviously

The only way I could make the array method to work was to declare (void)
in
the constructors, and create some member functions say, create(int, int,
int)
within A and B. It looks pretty ugly. 
 

Any toughts or hints?

Thx very much for your time in advance,
 
Brian Chan.
 
Stuttgart, FRG
          
--
=============================================================================
	Extended Bulletin Board Service, Research & Development
Office of Information Technology, University of North Carolina at Chapel Hill
	      internet: bbs.acs.unc.edu or 128.109.157.30

steve@taumet.com (Stephen Clamage) (03/29/91)

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.)
-- 

Steve Clamage, TauMetric Corp, steve@taumet.com

lijewski@theory.tn.cornell.edu (Mike Lijewski) (03/29/91)

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