overloading operators with private variables

From: R (ruthless_at_poczta.onet.pl)
Date: 12/29/04


Date: 29 Dec 2004 08:08:56 -0800

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