how do I overload (type) operator?

Discussion in 'C++' started by sean worlock, Oct 28, 2004.

  1. sean worlock

    sean worlock Guest

    I have written a simple class that modals the action of a complex number.
    This class is called CComplex.
    I wish to make my class as versatile as possable.
    Thus lets say I have some variables defined as following:

    int value1;
    double value2;
    float value3;

    and three Complex objects defined as:

    CComplex ONE,TWO,THREE;

    I wish to perform the following...

    ONE=(CComplex) value1;
    TWO=(CComplex) value2;
    THREE=(CComplex) value3;

    thus I need to create a type operator for my CComplex class.
    I have read many texts for the conversion of my class into other types but
    not the other way.

    could anybody point me in the correct direction for the prototype and
    function syntax to complete this?
    sean worlock, Oct 28, 2004
    #1
    1. Advertising

  2. "sean worlock" <> wrote in message
    news:clq4mr$rvm$...
    >I have written a simple class that modals the action of a complex number.
    > This class is called CComplex.
    > I wish to make my class as versatile as possable.
    > Thus lets say I have some variables defined as following:
    >
    > int value1;
    > double value2;
    > float value3;
    >
    > and three Complex objects defined as:
    >
    > CComplex ONE,TWO,THREE;
    >
    > I wish to perform the following...
    >
    > ONE=(CComplex) value1;
    > TWO=(CComplex) value2;
    > THREE=(CComplex) value3;
    >
    > thus I need to create a type operator for my CComplex class.
    > I have read many texts for the conversion of my class into other types but
    > not the other way.
    >
    > could anybody point me in the correct direction for the prototype and
    > function syntax to complete this?
    >


    Just define constructor:

    CComplex::CComplex( double value).

    The statements can be then rewritten like:

    ONE=value1;
    TWO=value2;
    THREE=value3;

    Catalin
    Catalin Pitis, Oct 28, 2004
    #2
    1. Advertising

  3. sean worlock wrote:

    > I have written a simple class that modals the action of a complex number.
    > This class is called CComplex.
    > I wish to make my class as versatile as possable.
    > Thus lets say I have some variables defined as following:
    >
    > int value1;
    > double value2;
    > float value3;
    >
    > and three Complex objects defined as:
    >
    > CComplex ONE,TWO,THREE;
    >
    > I wish to perform the following...
    >
    > ONE=(CComplex) value1;
    > TWO=(CComplex) value2;
    > THREE=(CComplex) value3;
    >
    > thus I need to create a type operator for my CComplex class.
    > I have read many texts for the conversion of my class into other types but
    > not the other way.
    >
    > could anybody point me in the correct direction for the prototype and
    > function syntax to complete this?


    You can template the the hole class:

    template<typename UnderLyingType>
    class CComplex

    Mayby you'd have to check if the type is a correct one, such as double
    and so on.

    HTH && Kind regards,
    Nicolas
    Nicolas Pavlidis, Oct 28, 2004
    #3
  4. sean worlock

    Tom Widmer Guest

    On Thu, 28 Oct 2004 09:51:48 +0300, "Catalin Pitis"
    <> wrote:

    >
    >"sean worlock" <> wrote in message
    >news:clq4mr$rvm$...
    >>I have written a simple class that modals the action of a complex number.
    >> This class is called CComplex.
    >> I wish to make my class as versatile as possable.
    >> Thus lets say I have some variables defined as following:
    >>
    >> int value1;
    >> double value2;
    >> float value3;
    >>
    >> and three Complex objects defined as:
    >>
    >> CComplex ONE,TWO,THREE;
    >>
    >> I wish to perform the following...
    >>
    >> ONE=(CComplex) value1;
    >> TWO=(CComplex) value2;
    >> THREE=(CComplex) value3;
    >>
    >> thus I need to create a type operator for my CComplex class.
    >> I have read many texts for the conversion of my class into other types but
    >> not the other way.
    >>
    >> could anybody point me in the correct direction for the prototype and
    >> function syntax to complete this?
    >>

    >
    >Just define constructor:
    >
    >CComplex::CComplex( double value).
    >
    >The statements can be then rewritten like:
    >
    >ONE=value1;
    >TWO=value2;
    >THREE=value3;


    Note that if you (the OP) want to require the cast notation, you can
    declare the constructor "explicit". e.g.

    explicit CComplex(double value); //don't use explicit in definition

    I can't think of a good reason why you would want to do this though.
    Also note that there is a perfectly good complex number class that is
    part of the standard library: std::complex<double>, declared in the
    <complex> header.

    Tom
    Tom Widmer, Oct 28, 2004
    #4
    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. Mr Smith
    Replies:
    7
    Views:
    457
    Mr Smith
    Feb 10, 2005
  2. Piotre Ugrumov
    Replies:
    3
    Views:
    362
    Nick Hounsome
    Jan 25, 2004
  3. Fabio Fracassi
    Replies:
    5
    Views:
    323
  4. Martin T.
    Replies:
    7
    Views:
    807
    Martin T.
    Mar 10, 2008
  5. Ying-Chieh Liao

    function overload (not operator overload)

    Ying-Chieh Liao, Oct 11, 2004, in forum: Perl Misc
    Replies:
    3
    Views:
    237
    Sherm Pendley
    Oct 11, 2004
Loading...

Share This Page