L B wrote:
|... and MyArea is just a variable to put the answer in, rather than returning a value from a function.|
|That's counter-intuitive, why would a getter have that semantic?|
I don't see what 'being restricted to one return type' has to do with what I'm trying to say here.
Well the idea was to provide the user with a choice of type for the value they were after. To do this I had overloaded functions. In order to overload them I had references to different types as parameters.
has just said (& me a couple of times, earlier) templates would be better. And Cubbi
's example of the boost library much earlier.
I guess the reason why one might want different types is, that at some point a calculation needs to be done, so we need a variable of a particular type to do it with. And we might need multiple types, as in a CAD system which has a circular selection area (unsigned) as well a Circle dwg object (double say) .
Also, one of main reasons that prompted this topic was the idea mentioned in the Java world article link posted by ne555
paraphrased as this: "If the type changes in the future then all the client code that uses it is broken "
Sometimes tutorials provide simplistic examples because they are aimed at beginners. Unfortunately they are so simplistic that they actually promote bad practice. The example you posted is bad IMO mainly because it has the setter function instead of a constructor with an initialiser list. As mentioned earlier, if one wants to change the values after initialisation, then think about why, and provide well behaved functions that implement this.
Having said that, the vast majority of example code in the reference section on this site is very useful & educational.