operator conversion

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

  1. John Cho

    John Cho Guest

    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));
     
    John Cho, Mar 3, 2004
    #1
    1. Advertising

  2. * 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 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.
     
    Alf P. Steinbach, Mar 3, 2004
    #2
    1. Advertising

  3. John Cho

    John Cho Guest

    (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
    #3
  4. * 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
    #4
  5. John Cho

    John Cho Guest

    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 Cho, Mar 3, 2004
    #5
  6. * John Cho <> 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.
     
    Alf P. Steinbach, Mar 3, 2004
    #6
  7. John Cho

    Leor Zolman Guest

    On Wed, 03 Mar 2004 18:56:35 GMT, John Cho <> 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

    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
     
    Leor Zolman, Mar 3, 2004
    #7
  8. John Cho

    Mike Wahler Guest

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

    Sorry! :)

    -Mike
     
    Mike Wahler, Mar 3, 2004
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. ES Kim
    Replies:
    14
    Views:
    605
    Rob Williscroft
    Sep 27, 2004
  2. Arvid Requate
    Replies:
    2
    Views:
    1,024
    Alf P. Steinbach
    Jun 23, 2006
  3. hurcan solter
    Replies:
    3
    Views:
    754
    Cholo Lennon
    Aug 29, 2007
  4. , India
    Replies:
    2
    Views:
    506
    Fraser Ross
    Sep 15, 2009
  5. cronusf
    Replies:
    12
    Views:
    510
Loading...

Share This Page