# operator conversion

Discussion in 'C++' started by John Cho, Mar 3, 2004.

1. ### John ChoGuest

Class Cartesian
{ double x;
double y;
public:
Cartesian( ) { x = 0.0 ; y = 0.0;}
Cartesian(double x1, double y1)
{ x = x1; y = y1;}
};
class Polar
double angle;
public:
Polar( ) { radius = 0.0; angle = 0.0;}
Polar(double r, double a) { radius = r; angle = a;}
//Use operator conversion function to convert Polar to Cartesian
operator Cartesian( )
{ double xx, yy;
return Cartesian(xx, yy);
}
};

why is Cartesian Cartesian(xx,yy); legal but float float(5.0); is not
legal

also why is it returning a constructor and not say

return (Cartesian Cartesian(xx, yy));

or
return (Cartesian C(xx, yy));

John Cho, Mar 3, 2004

2. ### Alf P. SteinbachGuest

* John Cho <> schriebt:
> Class Cartesian
> { double x;
> double y;
> public:
> Cartesian( ) { x = 0.0 ; y = 0.0;}

Style: use initializer lists.

> Cartesian(double x1, double y1)
> { x = x1; y = y1;}

Style: use initializer lists.

> };

Design: no way to use class Cartesian in any meaningful way.

> class Polar
> double angle;
> public:
> Polar( ) { radius = 0.0; angle = 0.0;}

Style: use initializer lists.

> Polar(double r, double a) { radius = r; angle = a;}

Style: use initializer lists.

> //Use operator conversion function to convert Polar to Cartesian
> operator Cartesian( )

Should be 'const'.

> { double xx, yy;
> xx = radius * cos(angle);
> yy = radius * sin(angle);
> return Cartesian(xx, yy);
> }
> };

Design: no way to use class Polar in any meaningful way.

> why is Cartesian Cartesian(xx,yy); legal

It might be or not. If it is then it introduces a variable named
'Cartesian' of type 'Cartesian'. Which is absolutely not a good idea.

> but float float(5.0); is not legal

'float' is a reserved word and so cannot be used to name things.

> also why is it returning a constructor

Why is _what_ returning something?

But no matter what you're referring to, it's not possible to return
a constructor.

E.g., Polar:perator Cartesian() returns an object of type Cartesian,
where this object is constructed by a call to the Cartesian constructor.

> and not say
>
> return (Cartesian Cartesian(xx, yy));

Huh.

> or
> return (Cartesian C(xx, yy));

Huh.

Alf P. Steinbach, Mar 3, 2004

3. ### John ChoGuest

(Alf P. Steinbach) wrote in news:40462599.396007500
@news.individual.net:

dude, why did you reply to my newsgroup message, i asked two specific
questions. why did you talk about style, then you did not even answer the
questions.

Damn dude, you just waste people's time doing that

John Cho, Mar 3, 2004
4. ### Alf P. SteinbachGuest

* John Cho <> schriebt:
> (Alf P. Steinbach) wrote in news:40462599.396007500
> @news.individual.net:
>
>
> why did you talk about style

Style is important in order to produce correct, maintainable and
efficient code.

In the code you presented all three concerns apply.

> then you did not even answer the questions.

I did.

> Damn dude, you just waste people's time doing that

Evidently I wasted my time, yes.

Alf P. Steinbach, Mar 3, 2004
5. ### John ChoGuest

improvements okay. damn, email me in the future if you want to discuss off
topic of the messages i orignally post okay.

John Cho, Mar 3, 2004
6. ### Alf P. SteinbachGuest

* John Cho <> schriebt:

The questions you posted were answered fully, to the extent possible.

You need to study that.

Alf P. Steinbach, Mar 3, 2004
7. ### Leor ZolmanGuest

On Wed, 03 Mar 2004 18:56:35 GMT, John Cho <> wrote:

>improvements okay. damn, email me in the future if you want to discuss off
>topic of the messages i orignally post okay.

John,
Chill out a moment. If you spend a bit of time looking over other threads
in this and similar groups (comp.lang.c, alt.comp.lang.learn.c-c++, for
example), you'll see that posting code is tantamount to an open invitation
to have that code critiqued, and when folks like Alf contribute their time
to go over your code and point out all the stylistic (and often much more
than "just" stylistic) issues, you might want to take the opportunity to
learn from that rather than getting defensive.

well. The other question is so mired in fundamental misunderstanding of
what construction and operator conversion (?) are, that there's no way to
even tell what you were thinking when you asked the question. Thus, he
showed you things that /were/ clearly explainable.

No one's out to ride your butt, here, but if you can't handle constructive
feedback then I'd suggest you refrain from posting code.
-leor

Leor Zolman
BD Software

www.bdsoft.com -- On-Site Training in C/C++, Java, Perl & Unix
Decryptor at www.bdsoft.com/tools/stlfilt.html

Leor Zolman, Mar 3, 2004
8. ### Mike WahlerGuest

"Leor Zolman" <> wrote in message
news:...

> No one's out to ride your butt, here, but if you can't handle constructive
> feedback then I'd suggest you refrain from posting code.

#include <stdbitch.h>

voyeur mai_not()
{