Topic: typdef does not define new types ?!?
Author: Stuart I Reynolds <S.I.Reynolds@cs.bham.ac.uk>
Date: 1997/10/03 Raw View
Hmm,
Why-o-why-o-why doesn't typedef define a new type?
E.g.
//-------------------------------------
typdef celsius float;
typdef kelvin float;
typdef fahernheit float;
class temperature
{
private:
float the_temp; //value in C K or F
public:
//functions to set the internal temperature representation
void set_temp(celcius new_temp);
void set_temp(kelvin new_temp);
void set_temp(fahernheit new_temp);
};
//-------------------------------------
Each set_temp function gets turned into void set_temp(float) and so we
get 2 "multiple definition of `void set_temp(float)'" compiler errors.
Is typedef implemented as a macro in most compilers?
Where is the type safety in this then eh?
Grrrrrr.
Stuart Reynolds
---
[ comp.std.c++ is moderated. To submit articles: try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
[ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu ]
Author: James Kuyper <kuyper@wizard.net>
Date: 1997/10/04 Raw View
Stuart I Reynolds wrote:
>
> Hmm,
>
> Why-o-why-o-why doesn't typedef define a new type?
>
> E.g.
>
> //-------------------------------------
>
> typdef celsius float;
> typdef kelvin float;
> typdef fahernheit float;
>
> class temperature
> {
> private:
>
> float the_temp; //value in C K or F
>
> public:
>
> //functions to set the internal temperature representation
> void set_temp(celcius new_temp);
> void set_temp(kelvin new_temp);
> void set_temp(fahernheit new_temp);
> };
>
> //-------------------------------------
>
> Each set_temp function gets turned into void set_temp(float) and so we
> get 2 "multiple definition of `void set_temp(float)'" compiler errors.
> Is typedef implemented as a macro in most compilers?
>
> Where is the type safety in this then eh?
>
> Grrrrrr.
>
Typedefs provide simpler ways of referring to existing types, they don't
really create new types. Why? I'm not enough of a language historian to
answer that question.
However, you can get the type safety you want by defining these as named
structures with one 'float' member each, rather than using typedefs.
That is undeniably clumsy, but it has the advantage that you can declare
conversion members for each one to one of the other types, constructors
with a 'float' argument, and 'float get_*();' members for each
temperature scale. A good compiler will produce almost the same code
with this technique as you could have gotten if typedef's really defined
new types.
Your temperature class seems to lack an important feature; an
enumeration type that indicates which scale the_temp is measured in.
---
[ comp.std.c++ is moderated. To submit articles: try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
[ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu ]
Author: stephen.clamage_nospam@eng.sun.com (Steve Clamage)
Date: 1997/10/04 Raw View
On 03 Oct 97 18:46:53 GMT, Stuart I Reynolds
<S.I.Reynolds@cs.bham.ac.uk> wrote:
>
> Why-o-why-o-why doesn't typedef define a new type?
Two reasons:
1. Compatibility with C. (You might ask why C made this choice. I
don't know the answer, but I would guess it was to keep the language
simpler. C doesn't have user-defined first-class types at all.)
2. C++ has a mechanism to define new first-class types: You define a
class if you want a new type. A typedef merely provides an alternative
way to spell the name of an existing type.
>Is typedef implemented as a macro in most compilers?
It better NOT be implemented as a text macro in any compiler, since
that provides the wrong semantics.
In a typical implementation, every type has an entry in the symbol
table. When you create a typedef, the typedef name refers to the same
entry as the type it aliases. (That's oversimplified, but it's the
general idea.)
---
Steve Clamage, stephen.clamage_nospam@eng.sun.com
( Note: remove "_nospam" when replying )
---
[ comp.std.c++ is moderated. To submit articles: try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
[ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu ]