Thread: Operator overloading.......

  1. #1
    Registered User
    Join Date
    Oct 2004
    Posts
    30

    Operator overloading.......

    Code:
    #include <iostream>
    #include <conio.h>
    #include <string>
    using namespace std;
    const int N=100;
    class A
    {
            private:
                    int ar[N];
            public:
                    A();
                    friend istream& operator>>(istream&,A &);
                    friend ostream& operator<<(ostream&,A &);
                    A& operator+(A &);
                    friend A operator-(A &,A &);
    };
    
    A :: A()
    {
        int i;
        for(i=0;i<N;i++)
            ar[i]=0;
    }
    
    istream& operator>>(istream& in,A &a)
    {
        int i;
        for(i=0;i<N;i++)
                in>>a.ar[i];
        return in;
    }
    
    ostream& operator<<(ostream& out,A &a)
    {
        int i;
        for(i=0;i<N;i++)
            out<<a.ar[i];
        out<<endl;
        return out;
    }
    
    A& A :: operator+(A &a)
    {
       A a1;
       int i=0;
    
       cout<<"inside operator+.."<<endl;
       for(i=0;i<N;i++)
          a1.ar[i] = a.ar[i];
       for(i=0;i<N;i++)
          cout<<a1.ar[i]<<" ";
       cout<<a1;
                return a1;
       return a1;
    }
    
    int main()
    {
        A a,b,c;
        cout<<"Enter ...";
        cin>>a>>b;
        cout<<"You entered...";
        cout<<a<<b;
    
        cout<<"Arithmetic"<<endl;
        c = a + b;
        cout<<endl;
        cout<<"After adding   : "<<c<<endl;
    
        getch();
        return 0;
    }
    And the problems r.........
    reference from a local variable 'a1' returned........

    i know that the scope of variable dies once the control lands outside the function............
    but as it is required to return the object......i don't get it .......
    how to do that?

    please help.......
    Syra
    Amateur's urge to master C/C++

  2. #2
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Code:
    A& operator+(A &);
    This is the binary operator +, as such it should return an object so the prototype should be:
    Code:
    A operator+(A &);

  3. #3
    Registered User Micko's Avatar
    Join Date
    Nov 2003
    Posts
    715
    I agree with Thantos. You must return a copy of object not a reference. Returning a reference is a disaster.
    Object a1 is local to a function nad after "program flow" left the function object goes out of the scope and it is destroyed. So basically you return reference to an object that doesn't exists any more. You sholud be very careful when dealing with these issues.

    O, and one advice: all warning messages treat like errors. Think why you're getting them and how to avoid them.

  4. #4
    Registered User
    Join Date
    Sep 2004
    Posts
    719
    agreed....warning messages are very important
    i seem to have GCC 3.3.4
    But how do i start it?
    I dont have a menu for it or anything.

  5. #5
    Registered User
    Join Date
    Oct 2004
    Posts
    30
    i have a query.....
    what is preffered for binary operators to be overloaded
    1. member function..
    2. friend function..
    Syra
    Amateur's urge to master C/C++

  6. #6
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,090
    Non-member, non-friend is best, but prefer friend to member.

  7. #7
    Registered User Micko's Avatar
    Join Date
    Nov 2003
    Posts
    715
    Well it's a tricky question. A lot of people argue about these issues.
    Often doesn't make much difference. Some times, depending upon the class design, the nonmember version may have an advantage, particularly if you have defined type conversions for the class

  8. #8
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    That would depend on who you ask and what their position is on OOP.

    For me I'd say: You only need to implement a few math functions (such as +=, *=, %=, and maybe -= /=). These should be member functions. The rest should use these functions and could easily be non member functions.

    Example:
    Code:
    class A
    {
      // Some variables here
      public:
        // Constructors
        A & operator += (const A & p ) // binary +
        {
          // Code to add p to *this
          return *this;
        }
        A operator - () // uniary -
        {
          // code to change the sign
          return *this;
        }
    };
    Now to do binary - I could simply have a non member function (doesn't even need to be friend such as
    Code:
    A operator - (const A &l, const A &r)
    {
      return l + (-r);
    }
    To do binary +
    Code:
    A operator + (const A &l, const A &r)
    {
      return A(l) += r;  // use the copy constructor to make a copy and add r to it.
    }

Popular pages Recent additions subscribe to a feed