[comp.lang.c++] Can I hide data from my compiler?

sorensen@hstbme.mit.edu (Gregory Sorensen) (03/28/89)

Howdy.  This is a major c++ neophyte here.  I have a question,
and am looking to the great collective intellect of the net 
for a solution (pointless, you say?? :-)

My problem:  I'd like to hide my data type from my routines.  
Specifically, I'd like routines which I can pass a data object
made up of, say, floats or shorts, and use the same identical
code to process either, although I won't know _until run time_
whether the data is made up of floats or shorts.  Is there a way
to do this other than defining functions for each data object?

Thanks,

Greg Sorensen
sorensen@hstbme.mit.edu

wietse@wzv.UUCP (Wietse Z. Venema) (03/28/89)

In article <10133@bloom-beacon.MIT.EDU> sorensen@hstbme.mit.edu.UUCP (Gregory Sorensen) writes:
>
>My problem:  I'd like to hide my data type from my routines.  
>Specifically, I'd like routines which I can pass a data object
>made up of, say, floats or shorts, and use the same identical
>code to process either, although I won't know _until run time_
>whether the data is made up of floats or shorts.  Is there a way
>to do this other than defining functions for each data object?

Once upon a time I did something like that (not in C++, however, just in
plain C).  It began as a set of printf()/scanf() functions for C structs.  
A data structure was described by a format string, such as

	f[10]	array of 10 floats
	{dff}	structure with one int and two floats

and so on.  The implementation involved numerous pointer casts, as you
may expect, and some knowledge about the alignment properties of code
generated by the compiler.  This worked fine for simple data structures
such as arrays, vectors and other tables.  Support for self-referential
types (linked lists or trees) was never implemented.

Another approach would be to mechanically generate the proper read-
write functions from the data declarations themselves.  This idea was
expressed last October by Eric Raymond <eric@snark.UUCP>.  Perhaps he
can comment on the status of this approach.
-- 
work:	wswietse@eutrc3.uucp	| Eindhoven University of Technology
work:	wswietse@heitue5.bitnet	| Mathematics and Computing Science
home:	wietse@wzv.uucp		| 5600 MB Eindhoven, The Netherlands

henry@utzoo.uucp (Henry Spencer) (03/29/89)

In article <10133@bloom-beacon.MIT.EDU> sorensen@hstbme.mit.edu.UUCP (Gregory Sorensen) writes:
>Specifically, I'd like routines which I can pass a data object
>made up of, say, floats or shorts, and use the same identical
>code to process either, although I won't know _until run time_
>whether the data is made up of floats or shorts.  Is there a way
>to do this other than defining functions for each data object?

Basically, no, although some languages (not C++) will do some of the
work of defining the functions for you.  The "same identical code" is
not identical at the machine level:  different machine instructions
are used to manipulate the different data types, so there *have* to be
separate functions, or the equivalent, down there somewhere.  C++ lets
you hide this from the outside world, and clever use of the C preprocessor
might largely mechanize generating the functions, but basically C++ as it
is currently defined isn't a lot of help when it comes to writing the
code itself.  You're going to have to consult some separate indication
of what the data type is, and then invoke a suitable chunk of code.
-- 
Welcome to Mars!  Your         |     Henry Spencer at U of Toronto Zoology
passport and visa, comrade?    | uunet!attcan!utzoo!henry henry@zoo.toronto.edu