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

* Re: overloading operators with private variables
  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
  1 sibling, 0 replies; 3+ messages in thread
From: Vinzent 'Gadget' Hoefler @ 2004-12-29 17:22 UTC (permalink / raw)


R wrote:

> 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;

Yes, classical beginner's problem. ;-)

I suppose you have the according "with"-clauses in there, right?
Remember that "with" doesn't make anything visible, and that's the
problem: function testclass."*" is not visible, function Standard."*"
is (it always is), but expects different types of parameters, of
course. So there are three possible solutions:

1) objectMul := testclass."*"(object, object2)

It's clean, it says where it's coming from, it works. So it's perfect,
isn't it? --- No? You're right. In most cases you don't want that,
because if you would have wanted a function call that really looks like
a function call, you would have declared it that way. ;-)

So there's solution

2) Insert a "use type testclass.rec1" clause at the beginning to make
these _operations_ on the type directly visible. This is what I would
suggest and this is what one usually wants.

Well, there's still solution

3) Insert a "use testclass" clause at the beginning to make everything
from testclass directly visible. Most people consider this a bad idea,
and they're right, for different reasons. So I wouldn't recommend it
neither.

So for now it's best to forget about solution 3.


Vinzent.



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

* Re: overloading operators with private variables
  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
  1 sibling, 0 replies; 3+ messages in thread
From: Dave Thompson @ 2005-01-10  3:35 UTC (permalink / raw)


On 29 Dec 2004 08:08:56 -0800, "R" <ruthless@poczta.onet.pl> wrote:

> I was trying to overload my test record. For example the
> multiplication. <snip>
> 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 "*";
> 
C++ (and C) can return by value any fixed type at all _except_ an
array, whether allocated locally or not. (Fixed means in C++
polymorphic = Ada classwide types get sliced.) Returning a _pointer
to_ local space, or (C++ only) a reference to such local space, is an
error, not required to be caught by the compiler although some will
(notably GCC). In Ada the equivalent is diagnosed, because the pointer
type must have a scope larger than the variable, but if you go to the
trouble of overriding it the result is just as wrong.

- David.Thompson1 at worldnet.att.net



^ 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