www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1998/09/02/12:44:12

Message-ID: <003601bdd68e$a8ff0fb0$726f6f6f@c3nt2>
From: "Andrew Deren" <aderen AT interaccess DOT com>
To: "Jorge Bruno S. S. Morgado" <morgado AT mail DOT telepac DOT pt>, <djgpp AT delorie DOT com>
Subject: Re: help - type conversion
Date: Wed, 2 Sep 1998 11:26:14 -0500
MIME-Version: 1.0

>How do I convert some type of data that I have created to a standard
>data type of c++ ?
>
>For example, I have created a class complex, that is something like
>this:
>
>class complex
>{
> protected:
> double real;
> double imag;
>
> public:
> ...
> ...
>}
>
>Now, in the main I have:
>
>main()
>{
> complex x;
> complex y;
>
> x = (10, 4);
> y = (43, 45);
>
>/*  And now, I want x to be a double, I do not want x to be something
>like 2.2 + 0i, I want it ti be just a double 2.2, like this: */
>
> x = 2.2;
>}
>
>Is it possible to do this, how can I do it ?
>
I don't really know if that's what you mean.
You want x to become double instead of complex, right?
You can't do it, just like you can't change a double to an int.
You can make it hold the value of a real and overload the operators
so that when you do 
x = 2.2; // overload = operator and set imaginary part to 0. 
and when you have a double var:
double z = 2.8;
and you want to do some arithmatic:
double w = z * x; 
you will have to overload the operator for multiplication and 
in that case if imag part is zero just use the real part and
if it's not you might just throw and exception or handle
it in whatever way you want to.

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019