From: "R" <ruthless@poczta.onet.pl>
Subject: overloading operators with private variables
Date: 29 Dec 2004 08:08:56 -0800
Date: 2004-12-29T08:08:56-08:00 [thread overview]
Message-ID: <1104336536.856474.279370@f14g2000cwb.googlegroups.com> (raw)
Sorry to bother You again.
But as a newbie... I've got new problem.
I was trying to overload my test record. For example the
multiplication.
procedure Main is
object : testclass.rec1;
object2 : testclass.rec1;
objectMul : testclass.rec1;
begin
testclass.Create(object, 100);
testclass.Create(object2, 10);
objectMul := object * object2;
Put_Line(Integer'Image(testclass.Get(objectMul)));
end Main;
the "*" is(package testclass.adb):
function "*"(left, right: rec1) return rec1 is
tmp : Integer;
ret: rec1;
begin
tmp := left.field * right.field;
Create(ret, tmp);
return ret; -- I'm not sure if Ada permits returning locally
-- created objects, C++ don't permit it(well
references yes)...
end "*";
and the result of compilation is:
main.adb:34:29: invalid operand types for operator "*"
main.adb:34:29: left operand has private type "rec1" defined at
testclass.ads:2
main.adb:34:29: right operand has private type "rec1" defined at
testclass.ads:2
and the testclass.ads with rec1 and functions' prototypes:
package testclass is
type rec1 is tagged private;
procedure Create(this: out rec1; s: in Integer);
function Get(this: rec1) return Integer;
function "*"(left, right: rec1) return rec1;
private
type rec1 is tagged record
field: Integer;
end record;
end testclass;
What should I do to overload these objects?
Are there any mechanisms like C++ friends?
Can my variable field still be private?
and one thing about style ;-)
When displaying Integer or Float is it better to use the
Integer(Float)_Text_IO.Put or can I use Integer(Float)'Image with
standard Put?
-- best regards R
-- Ada is quite easy but object progrmming in Ada is certainly not easy
next reply other threads:[~2004-12-29 16:08 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2004-12-29 16:08 R [this message]
2004-12-29 17:22 ` overloading operators with private variables Vinzent 'Gadget' Hoefler
2005-01-10 3:35 ` Dave Thompson
replies disabled
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox