Thread: operators with array of pointers

  1. #1
    Registered User
    Join Date
    Jun 2011
    Posts
    19

    operators with array of pointers

    I have an array of objects

    Code:
    Object ob[10];
    And this operators

    Code:
    Object Object::operator+(const Object& ob)
    {
        Object c;
    
        c._a = _a + ob._a;
        c._b = _b + ob._b;
    
        return c;
    }
    
    istream& operator>>(istream& is, Object& ob)
    {
       double a, b;
    
       is >> a >> b;
       ob.set(a,b);
       return is;
    }
    Now I want to make an array of pointers like this

    Code:
    Object  *ob[10];
    
    ob[i] = new Object;
    But now I have to change my operators and i'm not beeing able to do that.
    Any help how to do it?

  2. #2
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Quote Originally Posted by Gotze View Post
    But now I have to change my operators and i'm not beeing able to do that.
    Any help how to do it?
    What exactly do you mean by 'change' ?

    Maybe the following syntax' is what you're looking for:
    Code:
    //1
    an_object = ob[i]->operator+(another_object);
    //or
    an_object = *(ob+i) + another_object;
    //2
    std::cin>>*(ob+i);

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by Gotze View Post
    But now I have to change my operators and i'm not beeing able to do that.
    What makes you think you have to change them?

    The only issue with them is that your operator+ should be declared const, and your operator>> should be declared static (which it may well be in your header file.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Overloading Array Operators[]
    By User Name: in forum C++ Programming
    Replies: 3
    Last Post: 01-27-2011, 02:01 AM
  2. Overloading unary operators using pointers
    By Niels_M in forum C++ Programming
    Replies: 21
    Last Post: 09-04-2010, 06:15 AM
  3. Replies: 4
    Last Post: 07-01-2009, 01:53 PM
  4. Overloaded Operators, and Pointers
    By esaptonor in forum C++ Programming
    Replies: 8
    Last Post: 11-30-2007, 05:01 PM
  5. Replies: 1
    Last Post: 10-21-2007, 07:44 AM