operators with array of pointers

This is a discussion on operators with array of pointers within the C++ Programming forums, part of the General Programming Boards category; I have an array of objects Code: Object ob[10]; And this operators Code: Object Object::operator+(const Object& ob) { Object c; ...

  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
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    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);
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,293
    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, 01: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, 04:01 PM
  5. Replies: 1
    Last Post: 10-21-2007, 07:44 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21