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=-1.3 required=5.0 tests=BAYES_00,INVALID_MSGID autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,3d313337c39c5dd5 X-Google-Attributes: gid103376,public From: Matthew Heaney Subject: Re: run-time type identification Date: 1998/09/03 Message-ID: #1/1 X-Deja-AN: 387484397 Sender: matt@mheaney.ni.net References: <01bdd72e$49ee66b0$f330ea9e@ukp03332> <01bdd73c$082f0230$f330ea9e@ukp03332> NNTP-Posting-Date: Thu, 03 Sep 1998 06:57:51 PDT Newsgroups: comp.lang.ada Date: 1998-09-03T00:00:00+00:00 List-Id: "Bob Fletcher" writes: > Yeah, this is the problem, there's no way of ensuring the safety of the > operation. No. If the actual type isn't in A2'Class, then you'll get Constraint_Error. > Also, in the code you give, are you certain that any record fields specific > to the class A2 would be copied by the assignment to the constant? It seems > likely to me that they would be lost. No. > I imagine that invoking Z.All, in your example, will return only the > class A part of the object, regardless of whether it is in fact an > object of class A2. No. The type cast to A2 gives you an A2 view of the object. If O really is of type A2 (or its descendent), then you'll get all the fields. The thing I don't like about Samuel's example is that it makes a copy of the object: > Y : constant A2 := A2 (Z.all); There is no need for a copy. Just do a "view conversion": Y : A2 renames A2 (Z.all); or better Y : A2'Class renames A2'Class (Z.all); > As you rightly say, there will be problems if you try to do this in a > situation where you don't know whether the object in question is of the > base class or one of it's derived classes, which is what would really be > useful. The C++ dynamic_cast operator returns a zero (null) pointer if the > object being pointed to is not of the correct class. No, there will be _no_ problems. This is Ada. If the actual type of Z.all isn't in A2'Class, then you'll get Constraint_Error.