🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.sys.mac.programmer.codewarrior
Article #1

Re: How to initialize an array of classes whose contructors take an

#1
From: Justin Vallon
Date: Sat, 29 Jul 1995 19:54
65 lines
2533 bytes
Path: holonet!colossus.holonet.net!news.sprintlink.net!gatech!usenet.eel.ufl.
      edu!news.mathworks.com!uunet!in2.uu.net!spcuna!news.columbia.edu!
      dialup139.cc.columbia.edu!user
From: jcv6@columbia.edu (Justin Vallon)
Newsgroups: comp.sys.mac.programmer.codewarrior,comp.lang.c++
Subject: Re: How to initialize an array of classes whose contructors take an 
         int array
Date: Sat, 29 Jul 1995 15:54:06 -0400
Organization: Columbia University
Message-ID: <jcv6-2907951554060001@dialup139.cc.columbia.edu>
References: <fuz-2407951836130001@lkf0177.deltanet.com> <3v8517$1d0@magpie.
            cdev.com>

[Sorry about the x-refs]

>In article <fuz-2407951836130001@lkf0177.deltanet.com>, Scott
(fuz@deltanet.com) says...
>
>[snip]
>
>:This worked out just fine.  Later in development, I needed to make a clas
>:called a piece.  Pieces are constructed using an int array and an int, and
>:a piece list is constructed using an array of pieces.  Here, the fun
>:began.
>:
>:Piece pList[] = { Piece({1,2,3}, 95), Piece({4,5,6}, 34), Piece({7,8,9},
12) };
>:So, is there any clean way of doing this?  (creating an on the fly array
>:of integers to pass to a constructor)

What about the following?

struct PieceInfo {
   int items[3];
   int value;
};

struct PieceInfo info[] = { {1, 2, 3, 95}, {4, 5, 6, 34}, ... };
int infoIndex = 0;
Piece pieces[3];

Let Piece::Piece construct from info[infoIndex++].  The order of
construction may be implementation dependent, but you will get each struct
PieceInfo constructed.

You may have to make sure info and infoIndex are initialized before
Piece::Piece is called.

If you have to initialize more than one array at different points, you
could use a struct PieceInfo *curPieceInfo.

struct PieceInfo info[] = { {1, 2, 3, 95}, {4, 5, 6, 34}, ... };
struct PieceInfo *curPieceInfo = info;

Piece::Piece(void) {
   fData = *curPieceInfo++;
}

void foo(void) {
   curPieceInfo = another_array;
   delete [] new Piece[10];
}

-Justin
jcv6@columbia.edu
Technical Details
Message-ID: <950729200136671@wem.org>
Path: arc1-ca.pugleaf.net!colossus.holonet.net!christ!justin.vallon
References: <fuz-2407951836130001@lkf0177.deltanet.com> <3v8517$1d0@magpie.

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!colossus.holonet.net!christ!justin.vallon
From: justin.vallon@wem.org (Justin Vallon)
Newsgroups: comp.sys.mac.programmer.codewarrior,comp.lang.c++
Subject: Re: How to initialize an array of classes whose contructors take an
Date: Sat, 29 Jul 1995 19:54:06 GMT
Message-ID: <950729200136671@wem.org>
Organization: World Evangelist Ministry (310) 398-7804
Distribution: world
References: <fuz-2407951836130001@lkf0177.deltanet.com> <3v8517$1d0@magpie.