Search code examples
c++overloadingconstantsoperator-keywordinsertion

Do objects Coming into the Overloaded << Operator Function have to be Constants?


I have a program for class I'm writing where we are overloading the operators. I'm not having much trouble with any but the insertion operator. The object being passed has an character array with characters stored it in. These characters are ASCII values that represent numbers, ACSII value 1 is representing 1. 2 is representing 2. I need to convert the char array back to the corresponding character now (1 = 49, 2 = 50, etc.) by adding '0' to all the elements in the array. I can't change the contents of the array because its the object is being passed in as a constant. My question is:

Do objects being passed into the overloaded insertion operator functions have to be passed in as constants?

I'm fairly positive that our professor wants us to convert this array in this function. We wrote this entire program previously, but used read() and write() functions instead of overloading insertion and extraction operators. We did the converting in the read and write functions and he mentioned using the same code for the previous assignment in this assignment, but using the overloaded operators instead. I was able to convert the the numbers in the array in the previous assignment because it wasn't a constant. Here's the code:

ostream & operator<< (ostream &Out, const MyFloat & x)
{
    int Counter=0;
    int PrintDigits=0;

    //FIGURE OUT HOW TO CONVERT BACK IN THIS FUNCTION

    for (int h=0; h<=MyFloat::MAXDIGIT-1; h++)//converts from number to character
    {
        x.Number[h] += '0';//ISSUE is here, can't change this b/c it's const
    }

    Out << "0.";

    for (int i=x.MAXDIGIT-1; i>=0 && x.Number[i] == '0'; --i)//counting right to left, until we reach the first non-zero number, this is used to print out the correct amount of numbers, to keep from printing trailing 0s
    {
        ++Counter;
    }

    PrintDigits = 19 - Counter;

    for (int j=0; j<=PrintDigits; ++j)
    {
        Out << x.Number[j];
    }

    return Out;
}

Solution

  • I figured it out. Instead of trying to change the const object coming in, I just converted it as it was being stored in ostream variable Out.

    ostream & operator<< (ostream &Out, const MyFloat & x)
    {
        int Counter=0;
        int PrintDigits=0;
    
        Out << "0.";
    
        if (x.NumberOfDigits != '0')
        {
            for (int i=x.MAXDIGIT-1; i>=0 && x.Number[i] == 0; --i)//counting right to left, until we reach the first non-zero number, this is used to print out the correct amount of numbers, to keep from printing trailing 0s
            {
                ++Counter;
            }
    
            PrintDigits = 19 - Counter;
    
            for (int j=0; j<=PrintDigits; ++j)
            {
                Out << (int) x.Number[j];
            }
        }
    
        else
            Out << "?";
    
        return Out;
    }
    

    As I said, this is not my choice, just what the assignment is asking me to do.