comp.lang.ada
 help / color / mirror / Atom feed
From: "Rego, P." <pvrego@gmail.com>
Cc: mailbox@dmitry-kazakov.de
Subject: Re: Constructors with multiple inheritance
Date: Tue, 8 Nov 2011 18:24:53 -0800 (PST)
Date: 2011-11-08T18:24:53-08:00	[thread overview]
Message-ID: <21605158.153.1320805494018.JavaMail.geo-discussion-forums@yqiu15> (raw)
In-Reply-To: <1gnrks1djlaok.1k0r5f8z9ylfx.dlg@40tude.net>

>    function Construct (Object_Reference : access Parent_Class)
>       return Parent_Class_Ptr;

I tried this approach in the following code, but it still complains about the null object. 

    -- test_pkg.ads
    package Test_Pkg is
       type Test_Class is tagged;
       type Test_Class_Ptr is access all Test_Class;

       function Construct (T : access Test_Class) return Test_Class_Ptr;

       task type Primary_Task (This : not null access Test_Class);

       type Test_Class is tagged limited
          record
             Info    : Integer := 0;
             Value   : Float;
             Primary : Primary_Task (Test_Class'Access);
          end record;
    end Test_Pkg;

    -- test_pkg.adb
    with Text_IO; use Text_IO;
    package body Test_Pkg is
       task body Primary_Task is
       begin
          loop
             Put_Line (Integer'Image (This.Info) & "__");
             delay 0.2;
          end loop;
       end Primary_Task;

       function Construct (T : access Test_Class) return Test_Class_Ptr is
          T_Ptr : constant Test_Class_Ptr := new Test_Class;
       begin
          T_Ptr.Info := T.Info + 1;
          T_Ptr.Value := 0.0;
          return T_Ptr;
       end Construct;
    end Test_Pkg;

    -- test_pkg_user.adb
    with Test_Pkg;
    with Text_IO; use Text_IO;

    procedure Test_Pkg_User is
       Test_Obj : Test_Pkg.Test_Class_Ptr;
    begin
       Test_Obj := Test_Obj.Construct;
       Put_Line ("Value = "&Integer'Image (Test_Obj.Info));
    end Test_Pkg_User;

The message I get is the following:
    c:\tst>gnatmake test_pkg_user.adb
    gcc -c test_pkg_rev623_user.adb
    test_pkg_user.adb:12:16: warning: null value not allowed here
    test_pkg_user.adb:12:16: warning: "Constraint_Error" will be raised at run time
    gcc -c test_pkg.adb
    gnatbind -x test_pkg_user.ali
    gnatlink test_pkg_user.ali

What am I still doing wrong? Thanks again.

Note: Sure if I put in test_pkg_user.adb
    Test_Obj : Test_Pkg.Test_Class_Ptr := new Test_Pkg.Test_Class; 
it returns me a build with no warning, but I think this way I allocate memory twice to the pointer (one in the call new Test_Pkg.Test_Class and the other in the construct method execution), right?



  parent reply	other threads:[~2011-11-09  2:25 UTC|newest]

Thread overview: 28+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-09-29 19:40 Constructors with multiple inheritance Rego, P.
2011-09-29 20:20 ` Dmitry A. Kazakov
2011-09-30  3:11   ` Rego, P.
2011-09-30  7:36     ` Dmitry A. Kazakov
2011-09-30 14:04       ` Rego, P.
2011-09-30 16:29         ` Robert A Duff
2011-09-30 19:14           ` Rego, P.
2011-09-30 16:42         ` Dmitry A. Kazakov
2011-09-30 19:42           ` Rego, P.
2011-10-06 12:46             ` Julian Leyh
2011-11-09  2:24           ` Rego, P. [this message]
2011-11-09  8:39             ` Dmitry A. Kazakov
2011-11-10  3:47               ` Rego, P.
2011-11-10  7:09                 ` AdaMagica
2011-11-10  7:20                   ` AdaMagica
2011-11-10  8:35                   ` Dmitry A. Kazakov
2011-11-12 15:16                     ` Rego, P.
2011-11-12 15:30                   ` Rego, P.
2011-11-12 16:28                     ` Dmitry A. Kazakov
2011-11-12 17:41                       ` Rego, P.
2011-11-10  8:33                 ` Simon Wright
2011-11-10  9:01                 ` Georg Bauhaus
2011-11-10  9:09                   ` Georg Bauhaus
2011-11-10 18:16                 ` Jeffrey Carter
2011-11-10 19:39                   ` Dmitry A. Kazakov
2011-11-09  9:00             ` Simon Wright
2011-11-10  3:54               ` Rego, P.
2011-10-07  0:08 ` Shark8
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox