From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-2.9 required=5.0 tests=BAYES_00,MAILING_LIST_MULTI autolearn=unavailable autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,aa7f494bf30adbc7 X-Google-Attributes: gid103376,public Path: g2news1.google.com!news2.google.com!proxad.net!usenet-fr.net!enst.fr!melchior!cuivre.fr.eu.org!melchior.frmug.org!not-for-mail From: Stephen Leake Newsgroups: comp.lang.ada Subject: Re: [newbie] simple(?) data structures Date: 13 Jun 2004 10:47:08 -0400 Organization: Cuivre, Argent, Or Message-ID: References: <2j1e30Fsrg8vU1@uni-berlin.de> <878yesgvw5.fsf@insalien.org> <2j1v28Fs989jU1@uni-berlin.de> NNTP-Posting-Host: lovelace.ada-france.org Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Trace: melchior.cuivre.fr.eu.org 1087138049 67556 212.85.156.195 (13 Jun 2004 14:47:29 GMT) X-Complaints-To: usenet@melchior.cuivre.fr.eu.org NNTP-Posting-Date: Sun, 13 Jun 2004 14:47:29 +0000 (UTC) To: comp.lang.ada@ada-france.org Return-Path: In-Reply-To: <2j1v28Fs989jU1@uni-berlin.de> User-Agent: Gnus/5.09 (Gnus v5.9.0) Emacs/21.3 X-Virus-Scanned: by amavisd-new-20030616-p7 (Debian) at ada-france.org X-BeenThere: comp.lang.ada@ada-france.org X-Mailman-Version: 2.1.4 Precedence: list List-Id: "Gateway to the comp.lang.ada Usenet newsgroup" List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Xref: g2news1.google.com comp.lang.ada:1447 Date: 2004-06-13T10:47:08-04:00 Roland Illig writes: > Many thanks for your detailed answer. Now I know what I forgot to say. > The thing that makes the definition of the data structure that > complicated is that I want to choose the size of the Go_Board at > runtime and I want to pass that Go_Board to other subprograms. Is that > possible with packages? For a run-time determined size, the answer in Ada is the same as in C++; you need to allocate the board object after reading in the size. However, there are two ways to do this in Ada. The most powerful is with access types: package Go_Board is type size_Type is range 10 .. 50; type Board_Type (size : Size_Type) is tagged limited private; type Board_Access_Type is access Board_Type; ... end Go_Board; with Go_Board; with Ada.Command_Line; procedure Go_Main Size : constant Go_Board.Size_Type := Go_Board.Size_Type'Value (Ada.Command_Line.Argument (1)); Board : Go_Board.Board_Access_Type := new Go_Board.Board_Type (Size); begin -- play go. end Go_Main; This will allow you to change the size again after playing one game; deallocate Board (using an instance of Ada.Unchecked_Deallocation), and allocate it again. Then you have to be careful not to save a copy of the pointer to the board after the board is destroyed. The other way is to declare the board in a local block: with Go_Board; with Ada.Command_Line; procedure Go_Main Size : constant Go_Board.Size_Type := Go_Board.Size_Type'Value (Ada.Command_Line.Argument (1)); begin declare Board : Go_Board. Go_Board.Board_Type (Size); begin -- play go. end; end Go_Main; The second way doesn't need the access type; the board is "allocated" on the stack. But Board is only visible in the local block, which can be a problem for some applications. And, you can't change the size after playing one game. This might be a good way to start; learn about access types later. -- -- Stephe