CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11
  1. #1
    Join Date
    Mar 2010
    Posts
    5

    [RESOLVED] Operator Overloading on an Array

    Hi everyone,

    i would like to write operator overloading functions (like addition, subtraction etc) on a single dimension array in a class. The functions should be of type friend functions. .

    This is the function name:
    friend const Array operator +(const Array& array1);

    If the above piece of code is correct, what would be the remaining code for overloading the + function?

  2. #2
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Operator Overloading on an Array

    Quote Originally Posted by ahmad292 View Post
    Hi everyone,

    i would like to write operator overloading functions (like addition, subtraction etc) on a single dimension array in a class. The functions should be of type friend functions. .

    This is the function name:
    friend const Array operator +(const Array& array1);

    If the above piece of code is correct, what would be the remaining code for overloading the + function?
    Did you implement the addition logic? If not, no one knows how to do that except the person who knows what Array is and how it's implemented.

    Regards,

    Paul McKenzie

  3. #3
    Join Date
    Jan 2010
    Posts
    1,133

    Re: Operator Overloading on an Array

    Is that Array of yours by any chance the .NET Array class (System::Array)? If so, then you should know that "only the system and compilers can derive explicitly from the Array class. Users should employ the array constructs provided by the language."
    See:
    http://msdn.microsoft.com/en-us/libr...tem.array.aspx

    What are you trying to do, exactly? Attach two arrays? Add the values?
    (And, this question might seem strange, but: in what class? Array?)

  4. #4
    Join Date
    Sep 2004
    Location
    Holland (land of the dope)
    Posts
    4,123

    Re: Operator Overloading on an Array

    Is that Array of yours by any chance the .NET Array class (System::Array)? If so...
    ... then he would be in the wrong place, because this Codeguru section is for native C++

  5. #5
    Join Date
    Jan 2010
    Posts
    1,133

    Re: Operator Overloading on an Array

    Quote Originally Posted by Skizmo View Post
    ... then he would be in the wrong place, because this Codeguru section is for native C++
    ...but that does not prevent people from posting where they are not supposed to.

    That's why I said "by any chance"...

  6. #6
    Join Date
    Mar 2010
    Posts
    5

    Arrow Re: Operator Overloading on an Array

    Quote Originally Posted by Paul McKenzie View Post
    Did you implement the addition logic? If not, no one knows how to do that except the person who knows what Array is and how it's implemented.

    Regards,

    Paul McKenzie
    Apparently I should have had given more info.

    I'm designing a class called Array. This class has two private members, size (for size of the array) and *ptr (its declaration is: ptr = new int [size]; ). In the main, I want to create an array of type Array (object which is the class). The size of the array will be initialized through constructor (which is Array::Array(int mysize) ).

    Now suppose I want to add the contents of two arrays (Note: sizes of the two arrays may not be the same). How should I perform the addition of the two arrays using operator overloading?

    Public memeber of the addition function in the class would be:
    friend const Array operator +(const Array& array1);

  7. #7
    Join Date
    Mar 2010
    Posts
    5

    an example

    Arithmetic operation on array with different sizes. For e.g. Array a contains {1,2,3,4} and Array b contains {2,3,4,1,5,6} then the result of a+b is {3,5,7,5,5,6}.

  8. #8
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: Operator Overloading on an Array

    Quote Originally Posted by ahmad292
    Now suppose I want to add the contents of two arrays (Note: sizes of the two arrays may not be the same). How should I perform the addition of the two arrays using operator overloading?

    Public memeber of the addition function in the class would be:
    friend const Array operator +(const Array& array1);
    What do you mean by "add the contents of two arrays"? Do you mean to append one array to the end of the other to get a new array?

    By the way, whatever you mean by "add", you probably should consider overloading operator+= as a member function for your class, and then overload operator+ as a non-member non-friend function using that operator+=.

    EDIT:
    Quote Originally Posted by ahmad292
    Arithmetic operation on array with different sizes. For e.g. Array a contains {1,2,3,4} and Array b contains {2,3,4,1,5,6} then the result of a+b is {3,5,7,5,5,6}.
    Ah. In that case, you can do this using a non-member non-friend function. It is just a matter of creating a result Array that is a copy of the larger of the two Array objects, and then adding the corresponding elements of the other Array object to this result Array. It may be better to use a named function instead of overloading operator+.
    Last edited by laserlight; March 6th, 2010 at 01:10 PM.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  9. #9
    Join Date
    Mar 2010
    Posts
    5

    Re: Operator Overloading on an Array

    Quote Originally Posted by laserlight View Post

    EDIT:

    Ah. In that case, you can do this using a non-member non-friend function. It is just a matter of creating a result Array that is a copy of the larger of the two Array objects, and then adding the corresponding elements of the other Array object to this result Array. It may be better to use a named function instead of overloading operator+.
    I HAVE to implement the addition function using a friend function and overload operator+ . How can I do it using these preferences? May you please give a piece of code since I dont know how to implement it using a friend function?

  10. #10
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: Operator Overloading on an Array

    Quote Originally Posted by ahmad292
    I HAVE to implement the addition function using a friend function and overload operator+ .
    No, you don't have to have it as a friend, though if overloading operator+ is already settled as part of the interface, then so be it.

    Quote Originally Posted by ahmad292
    How can I do it using these preferences? May you please give a piece of code since I dont know how to implement it using a friend function?
    I already described how you can implement it (whether as operator+ or as a named function) as a non-member non-friend function. If you wish to insist on it being a friend, then it is your loss: How Non-Member Functions Improve Encapsulation.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  11. #11
    Join Date
    Mar 2010
    Posts
    5

    Resolved Re: Operator Overloading on an Array

    Thanks for the help everyone.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured