operator conversion

J

John Cho

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 radius;
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));
 
A

Alf P. Steinbach

* John Cho said:
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::eek: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.
 
J

John Cho

(e-mail address removed) (Alf P. Steinbach) wrote in @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
 
A

Alf P. Steinbach

* John Cho said:
(e-mail address removed) (Alf P. Steinbach) wrote in @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.
 
J

John Cho

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.
 
L

Leor Zolman

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 address 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
 
M

Mike Wahler

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

Sorry! :)

-Mike
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top