Re: ? Non-class typecast overloading

From: Igor Tandetnik (itandetnik_at_mvps.org)
Date: 09/16/04


Date: Thu, 16 Sep 2004 15:17:20 -0400


"Alec S." <a@a.com> wrote in message
news:uX2$5BBnEHA.1800@TK2MSFTNGP15.phx.gbl
> I am wondering if it is possible to overload typecasting for
> non-class types. For example is there a way to do something like
> this:
>
>
> /***************************/
> typedef int second;
> typedef int minute;
>
> operator second(minute min) {
> return min*60;
> }
>
> operator minute(second sec) {
> return sec/60;
> }

No, you can't do that. In C++, typedefs don't create new types, they
just introduce aliases to existing types. And there is no way to change
how fundamental types behave, or the language would be extremely
fragile.

However, see

http://www2.inf.ethz.ch/~meyer/publications/OTHERS/scott_meyers/dimensions.pdf
http://www.fnal.gov/docs/working-groups/fpcltf/html/SIunits-summary.html

and try finding C/C++ Users Journal, November 2002 issue for "Dimension
Checking of Physical Quantities" article.

-- 
With best wishes,
    Igor Tandetnik
"On two occasions, I have been asked [by members of Parliament], 'Pray,
Mr. Babbage, if you put into the machine wrong figures, will the right
answers come out?' I am not able to rightly apprehend the kind of
confusion of ideas that could provoke such a question." -- Charles
Babbage

Loading