|
-
June 20th, 2012, 05:31 AM
#31
Re: generic inheritance
Warnings were because of this reference (&)
Code:
NumericArray<Type>& NumericArray<Type>::operator *(double factor) const
{
NumericArray<Type> output(Array<Type>::Size());
for(int i=0; i<Array::Size(); i++)
{
output[i] = (*this)[i] * factor;
//return output;
}
return output;
}
#endif //NumericArray_h
SHOULD BE
Code:
NumericArray<Type> NumericArray<Type>::operator *(double factor) const
{
NumericArray<Type> output(Array<Type>::Size());
for(int i=0; i<Array::Size(); i++)
{
output[i] = (*this)[i] * factor;
//return output;
}
return output;
}
#endif //NumericArray_h
-
June 20th, 2012, 05:35 AM
#32
Re: generic inheritance
 Originally Posted by Paul McKenzie
The idea is this
You are to assume that you have no idea how the parent class makes copies.. This means that the only choice you have is to call the parent class assignment operator.
What if the parent class assignment operator changes? Are you going to fix your code trying to make those same changes in NumericArray? Why do that? Just call the parent assignment operator and let it do its work, whatever that work is.
When you make a copy, you're supposed to make sure that the parent make its copy too. Your code did not do that, instead you just copied and pasted what the parent class did. What if the assignment in the parent was a 1,000 line function? Are you going to copy and paste 1,000 lines of code in your NumericArray assignment operator?
You make the exact same mistake with your destructor -- why are you doing the work that the Array base class will do? Just let the Array class destructor do its job -- there is no need for you to duplicate code.
Regards,
Paul McKenzie
"You are to assume that you have no idea how the parent class makes copies.. This means that the only choice you have is to call the parent class assignment operator.
What if the parent class assignment operator changes? Are you going to fix your code trying to make those same changes in NumericArray? Why do that? Just call the parent assignment operator and let it do its work, whatever that work is. "
Those were crucial words thanks now I get the logic !!! really thanks... I read about this idea before but combination of words don't get through my brain so logically and understandable ...
-
June 20th, 2012, 05:37 AM
#33
Re: generic inheritance
 Originally Posted by oteel
Warnings were because of this reference (&)
Code:
NumericArray<Type>& NumericArray<Type>::operator *(double factor) const
{
NumericArray<Type> output(Array<Type>::Size());
for(int i=0; i<Array::Size(); i++)
{
output[i] = (*this)[i] * factor;
//return output;
}
return output;
}
#endif //NumericArray_h
SHOULD BE
Code:
NumericArray<Type> NumericArray<Type>::operator *(double factor) const
{
NumericArray<Type> output(Array<Type>::Size());
for(int i=0; i<Array::Size(); i++)
{
output[i] = (*this)[i] * factor;
//return output;
}
return output;
}
#endif //NumericArray_h
And the Array base class does exactly the same thing. So why are you duplicating the code here? Just call the base class version:
Code:
NumericArray<Type> NumericArray<Type>::operator *(double factor) const
{
Array<Type> output = Array<Type>::operator*(factor);
return output;
}
Regards,
Paul McKenzie
-
June 20th, 2012, 05:42 AM
#34
Re: generic inheritance
 Originally Posted by Paul McKenzie
And the Array base class does exactly the same thing. So why are you duplicating the code here? Just call the base class version:
Code:
NumericArray<Type> NumericArray<Type>::operator *(double factor) const
{
Array<Type> output = Array<Type>::operator*(factor);
return output;
}
Regards,
Paul McKenzie
Oh no you a little bit mistaken this is cool idea but in other time... because parents haven't realization of operator *
best regards
Last edited by oteel; June 20th, 2012 at 05:46 AM.
-
June 20th, 2012, 05:43 AM
#35
Re: generic inheritance
 Originally Posted by oteel
"You are to assume that you have no idea how the parent class makes copies.. This means that the only choice you have is to call the parent class assignment operator.
What if the parent class assignment operator changes? Are you going to fix your code trying to make those same changes in NumericArray? Why do that? Just call the parent assignment operator and let it do its work, whatever that work is. "
Those were crucial words thanks now I get the logic !!! really thanks... I read about this idea before but combination of words don't get through my brain so logically and understandable ...
Yes, hopefully you're understanding now.
What you were doing is that you were deriving, and not allowing the parent class to do any of its work.
Your NumericArray class just took over, thinking that it knew everything that the parent class did. You cannot assume this -- if a copy needs to be made, then you must make the parent class do the copy of its own data, otherwise you have a bad copy (you didn't make the parent class do its copying). If your program is making bad copies, then the program will demonstrate undefined behaviour.
Regards,
Paul McKenzie
-
June 20th, 2012, 05:44 AM
#36
Re: generic inheritance
 Originally Posted by oteel
The idea is that you make the base class do its work, and if there is any custom work, that is what you add to NumericArray.
Since there is no custom work, then the function becomes very simple.
Regards,
Paul McKenzie
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
|