* Cloning a Class Wide Data Type with "new"?
@ 2003-06-06 13:32 Warren W. Gay VE3WWG
2003-06-06 15:34 ` Jean-Pierre Rosen
` (3 more replies)
0 siblings, 4 replies; 8+ messages in thread
From: Warren W. Gay VE3WWG @ 2003-06-06 13:32 UTC (permalink / raw)
I havn't had much time to research this one yet, but I've
got stuck trying to clone a class wide type this morning,
on my way to work. A sample program is provided below.
I need to be able to allocate and then copy an arbitrary
tagged type (based upon a base type) into the newly
allocated object. There's gotta be a way to do this..
I just can't remember it!
The problem is the operation of the form of :
Cloned : Base_Access := new Base_Class'(Orig);
Here is a sample program that distills the problem:
with Ada.Text_IO;
use Ada.Text_IO;
procedure TProg is
type Base_Type is tagged
record
V : Integer;
end record;
type Base_Access is access all Base_Type'Class;
subtype Base_Class is Base_Type'Class;
type Derived_Type is new Base_Type with
record
Z : Natural;
end record;
-- This is the original object to be cloned
Orig : Derived_Type := ( V => 32, Z => 99 );
-- This is a local copy (easy)
Copied : Base_Class := Orig;
-- Allocate Cloned to be same as Orig (the problem)
Cloned : Base_Access := new Base_Class'(Orig);
begin
Put_Line("Cloned.V = " & Cloned.V'Img);
end TProg;
TIA, Warren.
--
Warren W. Gay VE3WWG
http://home.cogeco.ca/~ve3wwg
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: Cloning a Class Wide Data Type with "new"?
2003-06-06 13:32 Cloning a Class Wide Data Type with "new"? Warren W. Gay VE3WWG
@ 2003-06-06 15:34 ` Jean-Pierre Rosen
2003-06-06 17:15 ` Warren W. Gay VE3WWG
2003-06-06 18:11 ` David C. Hoos
` (2 subsequent siblings)
3 siblings, 1 reply; 8+ messages in thread
From: Jean-Pierre Rosen @ 2003-06-06 15:34 UTC (permalink / raw)
[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1: Type: text/plain, Size: 882 bytes --]
"Warren W. Gay VE3WWG" <ve3wwg@cogeco.ca> a �crit dans le message news:
3EE097D5.2010901@cogeco.ca...
> I havn't had much time to research this one yet, but I've
> got stuck trying to clone a class wide type this morning,
> on my way to work. A sample program is provided below.
>
> I need to be able to allocate and then copy an arbitrary
> tagged type (based upon a base type) into the newly
> allocated object. There's gotta be a way to do this..
> I just can't remember it!
>
> The problem is the operation of the form of :
>
> Cloned : Base_Access := new Base_Class'(Orig);
>
Cloned : Base_Access := new Base_Class'class'(Orig);
Hey, in Ada types must conform, and Base_Access is an access to
Base_Access'Class!
--
---------------------------------------------------------
J-P. Rosen (rosen@adalog.fr)
Visit Adalog's web site at http://www.adalog.fr
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: Cloning a Class Wide Data Type with "new"?
2003-06-06 15:34 ` Jean-Pierre Rosen
@ 2003-06-06 17:15 ` Warren W. Gay VE3WWG
0 siblings, 0 replies; 8+ messages in thread
From: Warren W. Gay VE3WWG @ 2003-06-06 17:15 UTC (permalink / raw)
Jean-Pierre Rosen wrote:
> "Warren W. Gay VE3WWG" <ve3wwg@cogeco.ca> a �crit dans le message news:
> 3EE097D5.2010901@cogeco.ca...
>
>>I havn't had much time to research this one yet, but I've
>>got stuck trying to clone a class wide type this morning,
>>on my way to work. A sample program is provided below.
>>
>>I need to be able to allocate and then copy an arbitrary
>>tagged type (based upon a base type) into the newly
>>allocated object. There's gotta be a way to do this..
>>I just can't remember it!
>>
>>The problem is the operation of the form of :
>>
>> Cloned : Base_Access := new Base_Class'(Orig);
>>
>
> Cloned : Base_Access := new Base_Class'class'(Orig);
BZZZzzt!
gcc -c -gnata -gnatf -gnato tprog.adb
tprog.adb:26:50: expected type "Base_Type'Class" defined at line 6
tprog.adb:26:50: found type "Derived_Type" defined at line 14
gnatmake: "tprog.adb" compilation error
> Hey, in Ada types must conform, and Base_Access is an access to
> Base_Access'Class!
You do understand what I am trying to do, right? I just cannot
seem to figure out how to specify it.
--
Warren W. Gay VE3WWG
http://home.cogeco.ca/~ve3wwg
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: Cloning a Class Wide Data Type with "new"?
2003-06-06 13:32 Cloning a Class Wide Data Type with "new"? Warren W. Gay VE3WWG
2003-06-06 15:34 ` Jean-Pierre Rosen
@ 2003-06-06 18:11 ` David C. Hoos
2003-06-06 18:18 ` Marc A. Criley
2003-06-06 18:38 ` Anh_Vo
3 siblings, 0 replies; 8+ messages in thread
From: David C. Hoos @ 2003-06-06 18:11 UTC (permalink / raw)
"Warren W. Gay VE3WWG" <ve3wwg@cogeco.ca> wrote in message
news:3EE097D5.2010901@cogeco.ca...
> I havn't had much time to research this one yet, but I've
> got stuck trying to clone a class wide type this morning,
> on my way to work. A sample program is provided below.
>
> I need to be able to allocate and then copy an arbitrary
> tagged type (based upon a base type) into the newly
> allocated object. There's gotta be a way to do this..
> I just can't remember it!
>
> The problem is the operation of the form of :
>
> Cloned : Base_Access := new Base_Class'(Orig);
>
> Here is a sample program that distills the problem:
>
>
> with Ada.Text_IO;
> use Ada.Text_IO;
>
> procedure TProg is
>
> type Base_Type is tagged
> record
> V : Integer;
> end record;
>
> type Base_Access is access all Base_Type'Class;
> subtype Base_Class is Base_Type'Class;
>
> type Derived_Type is new Base_Type with
> record
> Z : Natural;
> end record;
>
> -- This is the original object to be cloned
> Orig : Derived_Type := ( V => 32, Z => 99 );
>
> -- This is a local copy (easy)
> Copied : Base_Class := Orig;
>
> -- Allocate Cloned to be same as Orig (the problem)
> Cloned : Base_Access := new Base_Class'(Orig);
The operand of the qualified expression must first be converted to the
Base_Class -- i.e.:
Cloned : Base_Access := new Base_Class'(Base_Class (Orig));
See RM95 4.7 (3)
> begin
>
> Put_Line("Cloned.V = " & Cloned.V'Img);
>
> end TProg;
>
>
> TIA, Warren.
> --
> Warren W. Gay VE3WWG
> http://home.cogeco.ca/~ve3wwg
>
> _______________________________________________
> comp.lang.ada mailing list
> comp.lang.ada@ada.eu.org
> http://ada.eu.org/mailman/listinfo/comp.lang.ada
>
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: Cloning a Class Wide Data Type with "new"?
2003-06-06 13:32 Cloning a Class Wide Data Type with "new"? Warren W. Gay VE3WWG
2003-06-06 15:34 ` Jean-Pierre Rosen
2003-06-06 18:11 ` David C. Hoos
@ 2003-06-06 18:18 ` Marc A. Criley
2003-06-06 18:38 ` Anh_Vo
3 siblings, 0 replies; 8+ messages in thread
From: Marc A. Criley @ 2003-06-06 18:18 UTC (permalink / raw)
"Warren W. Gay VE3WWG" <ve3wwg@cogeco.ca> wrote in message news:<3EE097D5.2010901@cogeco.ca>...
> I need to be able to allocate and then copy an arbitrary
> tagged type (based upon a base type) into the newly
> allocated object. There's gotta be a way to do this..
> I just can't remember it!
>
> The problem is the operation of the form of :
>
> Cloned : Base_Access := new Base_Class'(Orig);
Here's what I did to get it to compile...
Cloned : Base_Access := new Base_Class'Class'(Base_Class(Orig));
...AND it seemed to work--though it'd be a little iffy for me to
explain in detail what's going on.
Marc A. Criley
Modified sample program follows:
with Ada.Text_IO;
use Ada.Text_IO;
procedure TProg is
type Base_Type is tagged
record
V : Integer;
end record;
type Base_Access is access all Base_Type'Class;
subtype Base_Class is Base_Type'Class;
type Derived_Type is new Base_Type with
record
Z : Natural;
end record;
-- This is the original object to be cloned
Orig : Derived_Type := ( V => 32, Z => 99 );
-- This is a local copy (easy)
Copied : Base_Class := Orig;
-- Allocate Cloned to be same as Orig (the problem)
Cloned : Base_Access := new Base_Class'Class'(Base_Class(Orig));
begin
Put_Line("Cloned.V = " & Cloned.V'Img);
Put_Line("Cloned.Z = " & Derived_Type(Cloned.all).Z'Img);
end TProg;
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: Cloning a Class Wide Data Type with "new"?
2003-06-06 13:32 Cloning a Class Wide Data Type with "new"? Warren W. Gay VE3WWG
` (2 preceding siblings ...)
2003-06-06 18:18 ` Marc A. Criley
@ 2003-06-06 18:38 ` Anh_Vo
2003-06-06 23:05 ` Anh_Vo
3 siblings, 1 reply; 8+ messages in thread
From: Anh_Vo @ 2003-06-06 18:38 UTC (permalink / raw)
"Warren W. Gay VE3WWG" <ve3wwg@cogeco.ca> wrote in message news:<3EE097D5.2010901@cogeco.ca>...
> I havn't had much time to research this one yet, but I've
> got stuck trying to clone a class wide type this morning,
> on my way to work. A sample program is provided below.
>
> I need to be able to allocate and then copy an arbitrary
> tagged type (based upon a base type) into the newly
> allocated object. There's gotta be a way to do this..
> I just can't remember it!
>
> The problem is the operation of the form of :
>
> Cloned : Base_Access := new Base_Class'(Orig);
>
> Here is a sample program that distills the problem:
>
>
> with Ada.Text_IO;
> use Ada.Text_IO;
>
> procedure TProg is
>
> type Base_Type is tagged
> record
> V : Integer;
> end record;
>
> type Base_Access is access all Base_Type'Class;
> subtype Base_Class is Base_Type'Class;
>
> type Derived_Type is new Base_Type with
> record
> Z : Natural;
> end record;
>
> -- This is the original object to be cloned
> Orig : Derived_Type := ( V => 32, Z => 99 );
>
> -- This is a local copy (easy)
> Copied : Base_Class := Orig;
>
> -- Allocate Cloned to be same as Orig (the problem)
> Cloned : Base_Access := new Base_Class'(Orig);
> begin
>
> Put_Line("Cloned.V = " & Cloned.V'Img);
>
> end TProg;
>
>
> TIA, Warren.
Base_Class is not a real type. Base_Type and Derived_Type are real
types. Thefore, either one of them should be used. I assume
Derived_Type was your intention in this case. Otherwise, explicit
conversion is needed. Both cases are shown below.
Cloned : Base_Access := new Derived_Type' (Orig); -- case 1
Cloned : Base_Access := new Base_Type' (Base_Type (Orig)); -- case 2
A. Vo
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: Cloning a Class Wide Data Type with "new"?
2003-06-06 18:38 ` Anh_Vo
@ 2003-06-06 23:05 ` Anh_Vo
2003-06-09 16:48 ` Warren W. Gay VE3WWG
0 siblings, 1 reply; 8+ messages in thread
From: Anh_Vo @ 2003-06-06 23:05 UTC (permalink / raw)
> Base_Class is not a real type. Base_Type and Derived_Type are real
> types. Thefore, either one of them should be used. I assume
> Derived_Type was your intention in this case. Otherwise, explicit
> conversion is needed. Both cases are shown below.
>
> Cloned : Base_Access := new Derived_Type' (Orig); -- case 1
>
> Cloned : Base_Access := new Base_Type' (Base_Type (Orig)); -- case 2
>
> A. Vo
My fingers were ahead of my thought. What I mean was that Base_Class
is class wide type while Base_Type and Derived_Type are not. That
means there should be case 3 for it.
Cloned : Base_Access := new Base_Class' (Base_Class (Orig)); -- case
3
By the way, I made another error while typing the word Therefore. But
it turned out to be Thefore.
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: Cloning a Class Wide Data Type with "new"?
2003-06-06 23:05 ` Anh_Vo
@ 2003-06-09 16:48 ` Warren W. Gay VE3WWG
0 siblings, 0 replies; 8+ messages in thread
From: Warren W. Gay VE3WWG @ 2003-06-09 16:48 UTC (permalink / raw)
Thank you everyone that replied to the "challenge".
Anh_Vo wrote:
>>Base_Class is not a real type. Base_Type and Derived_Type are real
>>types. Thefore, either one of them should be used. I assume
>>Derived_Type was your intention in this case. Otherwise, explicit
>>conversion is needed. Both cases are shown below.
>>
>>Cloned : Base_Access := new Derived_Type' (Orig); -- case 1
>>
>>Cloned : Base_Access := new Base_Type' (Base_Type (Orig)); -- case 2
>>
>>A. Vo
>
>
> My fingers were ahead of my thought. What I mean was that Base_Class
> is class wide type while Base_Type and Derived_Type are not. That
> means there should be case 3 for it.
>
> Cloned : Base_Access := new Base_Class' (Base_Class (Orig)); -- case
> 3
>
> By the way, I made another error while typing the word Therefore. But
> it turned out to be Thefore.
Yes, as pointed out by yourself and others, the above solution works.
Boiling it down to avoid using Base_Class (which is just Base_Type'Class),
this may be more easily(?) understood as:
Cloned : Base_Access := new Base_Type'Class'(Base_Type'Class(Orig));
which I guess makes sense. "Orig" is a specific type, so you need to
"convert" it to a class wide type to be used as the "prototype" to
be cloned. Then the "new" operator needs to be told to return a class
wide type, so I guess as clumsy as it looks, this is perhaps the only
conceivable way that it should be specified (ie. it makes sense).
Thanks everyone, Warren.
--
Warren W. Gay VE3WWG
http://home.cogeco.ca/~ve3wwg
^ permalink raw reply [flat|nested] 8+ messages in thread
end of thread, other threads:[~2003-06-09 16:48 UTC | newest]
Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2003-06-06 13:32 Cloning a Class Wide Data Type with "new"? Warren W. Gay VE3WWG
2003-06-06 15:34 ` Jean-Pierre Rosen
2003-06-06 17:15 ` Warren W. Gay VE3WWG
2003-06-06 18:11 ` David C. Hoos
2003-06-06 18:18 ` Marc A. Criley
2003-06-06 18:38 ` Anh_Vo
2003-06-06 23:05 ` Anh_Vo
2003-06-09 16:48 ` Warren W. Gay VE3WWG
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox