-
March 6th, 2010, 09:31 AM
#1
[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?
-
March 6th, 2010, 10:24 AM
#2
Re: Operator Overloading on an Array
Originally Posted by ahmad292
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
-
March 6th, 2010, 11:03 AM
#3
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?)
-
March 6th, 2010, 11:22 AM
#4
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++
-
March 6th, 2010, 11:32 AM
#5
Re: Operator Overloading on an Array
Originally Posted by Skizmo
... 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"...
-
March 6th, 2010, 01:01 PM
#6
Re: Operator Overloading on an Array
Originally Posted by Paul McKenzie
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);
-
March 6th, 2010, 01:03 PM
#7
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}.
-
March 6th, 2010, 01:06 PM
#8
Re: Operator Overloading on an Array
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:
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.
-
March 6th, 2010, 01:23 PM
#9
Re: Operator Overloading on an Array
Originally Posted by laserlight
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?
-
March 6th, 2010, 01:44 PM
#10
Re: Operator Overloading on an Array
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.
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.
-
March 6th, 2010, 01:53 PM
#11
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|