comp.lang.ada
 help / color / mirror / Atom feed
* overloading operators with private variables
@ 2004-12-29 16:08 R
  2004-12-29 17:22 ` Vinzent 'Gadget' Hoefler
  2005-01-10  3:35 ` Dave Thompson
  0 siblings, 2 replies; 3+ messages in thread
From: R @ 2004-12-29 16:08 UTC (permalink / 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




^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2005-01-10  3:35 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2004-12-29 16:08 overloading operators with private variables R
2004-12-29 17:22 ` Vinzent 'Gadget' Hoefler
2005-01-10  3:35 ` Dave Thompson

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