About operator overload?

Discussion in 'C++' started by jay, May 21, 2006.

  1. jay

    jay Guest

    In the c++ primer ,i get a program.
    A class's name is TT,and it define the operator overload!

    TT first; //constructor
    TT second(30);//constructor
    TT thrid(40://constructor
    first=second.operator+;

    the question is the fourth line is all right?

    maybe that is :
    first=second.operator+(third);

    which one is right?
     
    jay, May 21, 2006
    #1
    1. Advertising

  2. jay

    leconte Guest

    jay wrote:
    > In the c++ primer ,i get a program.
    > A class's name is TT,and it define the operator overload!
    >
    > TT first; //constructor
    > TT second(30);//constructor
    > TT thrid(40://constructor
    > first=second.operator+;
    >
    > the question is the fourth line is all right?
    >
    > maybe that is :
    > first=second.operator+(third);
    >
    > which one is right?
    >

    i think u r right:)
     
    leconte, May 21, 2006
    #2
    1. Advertising

  3. jay wrote:
    > In the c++ primer ,i get a program.
    > A class's name is TT,and it define the operator overload!
    >
    > TT first; //constructor
    > TT second(30);//constructor
    > TT thrid(40://constructor
    > first=second.operator+;
    >
    > the question is the fourth line is all right?


    Even the third line is not alright. The fourth line is definitely missing
    some parentheses.

    > maybe that is :
    > first=second.operator+(third);
    >
    > which one is right?


    Depending on how the operator is defined, either could be right.

    V
    --
    Please remove capital As from my address when replying by mail
     
    Victor Bazarov, May 21, 2006
    #3
  4. jay

    asterisc Guest

    < TT first; //constructor
    < TT second(30);//constructor
    < TT thrid(40://constructor
    < first=second.operator+;

    < the question is the fourth line is all right?

    < maybe that is :
    < first=second.operator+(third);

    < which one is right?

    For example:
    class TT
    {
    public:
    TT() : Value( 0 ) {}
    TT( int value ) : Value( value ) {}

    TT operator+() // first overloading
    {
    return TT( this->Value );
    }

    TT operator+ ( const TT& op ) // second overloading
    {
    return TT( this->Value + op.Value );
    }

    protected:
    int Value;
    };

    //----------------------------------------------------------------------------------
    int _tmain(int argc, _TCHAR* argv[])
    {
    TT first; //constructor
    TT second(30); //constructor
    TT third(40); //constructor

    first = second.operator+(); // first overloading (it
    maybe should be operator =)
    first = second + third; // second overloading
    std::cout << first.Value << std::endl;

    return 0;
    }

    This is perfect legally code!
    So, if at your 4-th statement, add some paranthesis, the code looks
    fine

    It's only depending on how your operator+ is defined!
     
    asterisc, May 21, 2006
    #4
  5. asterisc <> wrote:

    > int _tmain(int argc, _TCHAR* argv[])
    > {
    > TT first; //constructor
    > TT second(30); //constructor
    > TT third(40); //constructor
    >
    > first = second.operator+(); // first overloading (it
    > maybe should be operator =)
    > first = second + third; // second overloading
    > std::cout << first.Value << std::endl;
    >
    > return 0;
    > }
    >
    > This is perfect legally code!


    It's not because _TCHAR is not defined anywhere. Also are you
    missing a main function. _tmain is non-standard.

    regards
    --
    jb

    (reply address in rot13, unscramble first)
     
    Jakob Bieling, May 21, 2006
    #5
  6. jay

    asterisc Guest

    >> This is perfect legally code!
    >
    > It's not because _TCHAR is not defined anywhere. Also are you
    >missing a main function. _tmain is non-standard.


    Sorry, i use to work on windows platform, and i made a... quick
    project.
    It automaticaly includes:

    #include <iostream>
    #include <tchar.h>

    But, ignore that, and replace: _tmain with main, and _TCHAR with char
    ;)
    Anyways, the main focus was the overloaded operators, and their
    callings, not.. the main entry
     
    asterisc, May 22, 2006
    #6
  7. "jay" <> wrote in message
    news:...
    > In the c++ primer ,i get a program.
    > A class's name is TT,and it define the operator overload!
    >
    > TT first; //constructor
    > TT second(30);//constructor
    > TT thrid(40://constructor
    > first=second.operator+;
    >
    > the question is the fourth line is all right?


    You can find a way to make it work, if you correct the third line to TT
    third(40); and write a program like the following:

    #include <iostream>

    class TT
    {
    private:
    void (*m_p)(const TT&);
    public:
    TT()
    : m_p(NULL)
    {}

    TT(int n)
    : m_p(NULL)
    {}

    static void operator+(const TT& rhs) // dubious and contrived
    {
    std::cout << "Blah\n";
    }

    TT& operator=(void (*p)(const TT&))
    {
    m_p = p;
    return *this;
    }

    void f()
    {
    (*m_p)(*this);
    }
    };

    int main()
    {
    TT first;
    TT second(30);
    TT third(40);
    first = second.operator+;
    first.f();
    return 0;
    }

    Is it a good idea? Almost invariably not (never say "never", of course!) But
    it does go to show that it can be done if you really want to :)

    HTH,
    Stu

    > maybe that is :
    > first=second.operator+(third);
    >
    > which one is right?
     
    Stuart Golodetz, May 22, 2006
    #7
    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:
    468
    Mr Smith
    Feb 10, 2005
  2. Matthew Monopole

    overload operator -

    Matthew Monopole, Aug 8, 2003, in forum: C++
    Replies:
    1
    Views:
    478
    Jakob Bieling
    Aug 8, 2003
  3. Jakob Bieling

    Q: Overload operator new/delete

    Jakob Bieling, Aug 8, 2003, in forum: C++
    Replies:
    0
    Views:
    400
    Jakob Bieling
    Aug 8, 2003
  4. Piotre Ugrumov
    Replies:
    3
    Views:
    374
    Nick Hounsome
    Jan 25, 2004
  5. Ying-Chieh Liao

    function overload (not operator overload)

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

Share This Page