Velocity Reviews > C++ > operator conversion

# operator conversion

John Cho
Guest
Posts: n/a

 03-03-2004
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;
xx = radius * cos(angle);
yy = radius * sin(angle);
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));

Alf P. Steinbach
Guest
Posts: n/a

 03-03-2004
* John Cho <(E-Mail Removed)> 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 radius;
> 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.

John Cho
Guest
Posts: n/a

 03-03-2004
http://www.velocityreviews.com/forums/(E-Mail Removed) (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

Alf P. Steinbach
Guest
Posts: n/a

 03-03-2004
* John Cho <(E-Mail Removed)> schriebt:
> (E-Mail Removed) (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.

John Cho
Guest
Posts: n/a

 03-03-2004
your comments are not related to my question, i didn not ask about style
improvements okay. damn, email me in the future if you want to discuss off
topic of the messages i orignally post okay.

Alf P. Steinbach
Guest
Posts: n/a

 03-03-2004
* John Cho <(E-Mail Removed)> schriebt:
> your comments are not related to my question

What is your question?

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

> i didn not ask about style

You need to study that.

Leor Zolman
Guest
Posts: n/a

 03-03-2004
On Wed, 03 Mar 2004 18:56:35 GMT, John Cho <(E-Mail Removed)> wrote:

>your comments are not related to my question, i didn not ask about style
>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.

The only question you asked that made any sense to /me/, he answered quite
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
(E-Mail Removed)
www.bdsoft.com -- On-Site Training in C/C++, Java, Perl & Unix
C++ users: Download BD Software's free STL Error Message
Decryptor at www.bdsoft.com/tools/stlfilt.html

Mike Wahler
Guest
Posts: n/a

 03-03-2004
"Leor Zolman" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...

> 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()
{
}

Sorry!

-Mike