Re: Calling constructor

From: Victor Bazarov (v.Abazarov_at_comAcast.net)
Date: 02/12/04


Date: Thu, 12 Feb 2004 19:17:49 GMT


"Sylvain" <sylvain.boucher@aitb.org> wrote...
> Let's say I have the following code where a class 'pipo' has 8 instances
> of foo:
>
> class foo
> {
> foo ( const char * _name):
> name = _name
> {
>
> }
> private:
> const char * name;
> };
>
> class pipo
> {
> foo *mf[8];
> pipo()
> {
> for (unsigned int i = 0 ; i < 8; ++i)
> {
> ostringstream ost;
> ost << "MY_FOO_" << i;
> string name = ost.str();
> mf[i] = new foo (name.c_str());
> }
> }
> };
>
> Is it mandatory to do in such a way: using array of pointers on foo. Or
> may I do it by using plain array of instances of foo?
> In this case, I have no clue how to process to call the constructor with
> the correct C like string as defined in the exemple...
> Do you have any ideas how to proceed?
>
> Something like this?
>
> class pipo
> {
> foo mf[8];
> pipo ():
> ????
> {
>
> }
> };

There is no way to directly initialise an array if the array is
a non-static member of a class. Even with your 'array of pointers'
solution, the pointers are uninitialised in the beginning and then
are _assigned_ values in the constructor body. It's a shortcoming
of the language, nothing you or anybody else can do about it at
this time.

Victor



Relevant Pages