Overloaded operator += using pointer

From: Tony Johansson (johansson.andersson_at_telia.com)
Date: 04/30/04


Date: Fri, 30 Apr 2004 09:53:03 GMT

Hello Experts!!

Assume you have the following definition of class Array
class Array
{
public:
     Array (int antal=0);
     Array(const Array& v);
     const Array* operator+= (const Array& v)
     {
           for(int i=0; i<ant; i++)
                p[i] += v.p[i];
          returning (this); //here you are returning a pointer
     }
private:
     int *p;
     int ant;
};

In main you do the following
main()
{
     Array a1(5); //Instansiate the object a1 of class array
     Array a2(5) //Instansiate the object a2 of class array
// Assume you have code that load the dynamic array p in object a1 and
// object a2 with some values.
     a1 += a2; //calling the overloaded operator += which will return a
pointer
}

Now to my question the overloaded operator += will return a pointer because
the return type is
Array* but how can the receiving object which is a1 in main receive a
pointer(which is an address) when the object a1 is not declared as a
pointer.

I know I can use a reference instead so you don't have to mentioned anything
about that.!!

//Tony



Relevant Pages

  • Re: Overloaded operator += using pointer
    ... > Assume you have the following definition of class Array ... > pointer. ... take a pointer type(the return value from operator+=) ...
    (alt.comp.lang.learn.c-cpp)
  • Re: Copy Constructor and other questions
    ... Consider passing a pointer to a class derived from A??? ... Well, you can return a reference to NULL, but then the burden is on the ... > int main ...
    (comp.lang.cpp)
  • Re: my rant on value types and accessors
    ... you want S to reference sematics instead of value ... So, then, why are you make it a struct instead of a class? ... programmers for accessing structure members through a pointer. ... you need to have member (so they can access the private ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: A Question about pointer and reference and returning a value.
    ... > Assume you have the following definition of class Array ... advise you properly with these advanced topics. ...
    (alt.comp.lang.learn.c-cpp)