I'm writing a polynomial project that requires me to utilize classes. Problem is, I have the polynomial stored in two arrays in the class, one for coefficients (float coefficientholder[10]) and one for exponents (int exponentholder[10]), and get functions (at least according to my book) are simply consisting of return commands for use in the main .cpp file.
Since you can't exactly 'return' arrays with the return command, how I am supposed to return the array of coefficients and exponents when I use poly.getcoeff() and poly.getexpn() in the main.cpp file?
I wonder if you are actually using the exponent array. If that is the case, then the one of coefficients is worthless alone.
So, why do you want to return it? what operation must perform main with that information?
The coefficients method returns the reference to the private member theCoefficients. Since the return type is const std::vector<double> &, it cannot be used to change the value of theCoefficients. However a dishonest third party user can use const_cast<std::vector<double> &> to remove the const-ness and change the private member of the Polynomial class.
If you are too worried about this, then return a copy of theCoefficient. See the following code
1 2 3 4 5 6
public:
std::vector<double> coefficients (void) const
// Returning a copy of theCoefficients
{
return theCoefficients;
}
However this will be slow, as you are creating a copy of the original coefficient vector.