operator overloading...

Discussion in 'C++' started by Gernot Frisch, Aug 4, 2004.

  1. Hi,

    when Do I need the:

    a a::eek:perator+(a _1);
    and when the
    a operator+(a _1, a _2)

    ??



    --
    -Gernot
    int main(int argc, char** argv) {printf
    ("%silto%c%cf%cgl%ssic%ccom%c", "ma", 58, 'g', 64, "ba", 46, 10);}

    ________________________________________
    Looking for a good game? Do it yourself!
    GLBasic - you can do
    www.GLBasic.com
    Gernot Frisch, Aug 4, 2004
    #1
    1. Advertising

  2. Gernot Frisch wrote:
    > Hi,
    >
    > when Do I need the:
    >
    > a a::eek:perator+(a _1);


    This is a member function

    > and when the
    > a operator+(a _1, a _2)


    This is a non-member function.

    >
    > ??
    >
    >
    >


    I think this example might help clear things up a little.

    struct A
    {
    A operator + ( const A & );

    };


    struct B
    {
    };

    B operator + ( const B &, const B & );
    B operator + ( const B &, const A & );
    B operator + ( const A &, const B & );

    void foo()
    {

    B b1;
    B b2;
    A a1;
    A a2;

    b1 = b1 + b2;
    b1 = a1 + b1;
    b1 = b1 + a1;
    a1 = a1 + a2;
    }
    Gianni Mariani, Aug 4, 2004
    #2
    1. Advertising

  3. > I think this example might help clear things up a little.

    Not completly


    struct B;

    > struct A
    > {
    > A operator + ( const A & );


    // What about:
    A operator +(const B&);

    >
    > };
    >
    >
    > struct B
    > {
    > };
    >


    versus this one??
    > B operator + ( const A &, const B & );


    Thank you,
    Gernot
    Gernot Frisch, Aug 4, 2004
    #3
  4. Gernot Frisch wrote:
    >
    > Hi,
    >
    > when Do I need the:
    >
    > a a::eek:perator+(a _1);
    > and when the
    > a operator+(a _1, a _2)
    >
    > ??


    The first is a member function, while the second is not.

    The difference is as follows:

    Assume

    class A
    {
    public:
    A( int i ) : m_i( i ) {}

    private:
    int m_i;
    }

    That is: there is a class where objects can implicitely be constructed
    from an int.

    Now assume you want to write an op+ to add 2 A objects. You do this
    as member function

    class A
    {
    ....

    A operator+( const A& Arg ) { .... }
    };

    Now what can you do with it?

    You can eg. write

    A ObjA( 5 );
    A ObjB( 7 );
    A Result;


    Result = ObjA + ObjB;

    No problem. But you can also write

    Result = ObjA + 7;

    Why? Because the compiler can use the constructor to first convert 7
    into an A object and then use op+ to perform the addition.

    But can you also write

    Result = 7 + ObjA;

    And the answer is: No. Because 7 is an int, and there is no op+ for
    an int which takes an A object.

    This seems illogical and it can be cured by making op+ a non member
    function

    A operator+( const A& lhs, const A& rhs ) { ... }

    Now the compiler can use this operator as long as there is one A
    object on either side of the '+'. The other one will be converted
    to an A object if necessary.


    --
    Karl Heinz Buchegger
    Karl Heinz Buchegger, Aug 4, 2004
    #4
  5. > This seems illogical and it can be cured by making op+ a non member
    > function
    >
    > A operator+( const A& lhs, const A& rhs ) { ... }
    >
    > Now the compiler can use this operator as long as there is one A
    > object on either side of the '+'. The other one will be converted
    > to an A object if necessary.


    Now, I toally got this! Thank you a lot for you explaination.
    -Gernot
    Gernot Frisch, Aug 4, 2004
    #5
  6. Gernot Frisch

    Niels Dekker Guest

    Gernot Frisch asked:
    > when Do I need the:
    >
    > a a::eek:perator+(a _1);
    > and when the
    > a operator+(a _1, a _2)


    Karl Heinz Buchegger replied:
    > The first is a member function, while the second is not.

    ....
    > A operator+( const A& lhs, const A& rhs ) { ... }


    It has been suggested to implement operator+ in terms of member
    operator+= (e.g., last Friday by Richard Herring, on this newsgroup).
    Of course, you should have operator+= as follows:
    A& A::eek:perator+=(const A&)
    {
    // TODO: The addition itself!
    return *this;
    }

    If you do, is there a difference between the following two versions?

    A operator+(const A& _1, const A& _2)
    {
    return A(_1) += _2;
    }

    and:

    A operator+(A _1, const A& _2)
    {
    return _1 += _2;
    }

    Which one is preferable? Will it make any difference to the caller of
    operator+?


    Niels Dekker
    www.xs4all.nl/~nd/dekkerware
    Niels Dekker, Aug 4, 2004
    #6
    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. John Smith
    Replies:
    2
    Views:
    417
    Ivan Vecerina
    Oct 6, 2004
  2. Replies:
    11
    Views:
    727
    James Kanze
    May 16, 2007
  3. hurcan solter
    Replies:
    3
    Views:
    725
    Cholo Lennon
    Aug 29, 2007
  4. Replies:
    11
    Views:
    558
  5. Replies:
    2
    Views:
    311
Loading...

Share This Page