structure assignment, why you would want to do it

Mark A Terribile mat at mole-end.UUCP
Thu Aug 30 17:19:04 AEST 1990


>> :      Recall that "x -= y" is equivalent to "x = x - y".  Will your VAX C let
>> : you get away with "struct_thing = struct_thing - still_more_struct"?  How
>> : have you declared "struct_thing" that lets it both (a) be a structure, and 
>> : (b) be in the domain of subtraction, which normally works on *numbers*???

In other words, you want to define  operations  +  and  -  on struct types
or your own design.

> > As it happens, this facility is not present in C.  There is nothing in
> > the language which would make it especially difficult to implement, it's
> > just that nobody ever thought it worth while.  It was a sensible question.

Weelll ... what happens when you have stored in that struct not only
coordinates but also data that are not to be added, subtracted, &c?  Like,
for instance, a name string?  There's more to be done than the compiler
can do automatically.

> 	Thanx!  Everyone wondered why I asked it.  If you are doing
> transformations/similar math stuff or (like I'm doing) vehicle position
> updates for a military simulation, you'd like to be able to say
> 
> pres_location += current_move;
> 
> to update the vehicles location.  I could go through and assign the x,y,z
> locations individually, but it would be nice (and cleaner looking) to do a
> structure assignment.
>         It was intended that the assignement/operator would only be performed
> on like structure elements, and only on elements defined for the operater(s).

Well, OK, but what if your coordinates are polar?  Or direction cosines?
Or ... ?

I still think that you need a way to tell the compiler what the operators
mean when applied to these structs of yours.

>         Hmm, the next C standard is how many years away? :)

C is probably the wrong place to look for this stuff.  Expect new C standards
to be clarifications and tweaks and numerical stuff, and maybe conformant
array parameters.

The place to look (as if everyone hadn't by now guessed it) is C++, which
allows you to program just exactly what you have asked for here.  Yes,
really, even including the  +  and  -  operators.  And it's not years away,
it's here NOW.  Don't expect to get things for free; the step from C to
C++ is not a simple one.  If I have you in a classroom, we will spend 50
hours doing it, and you will spend at least 100 hours on assignments to
master the core of the language.

What your asking for isn't simple.  You can get it, but there is a price to
be paid.
-- 

 (This man's opinions are his own.)
 From mole-end				Mark Terribile



More information about the Comp.lang.c mailing list