简体   繁体   中英

Passing an object through a constructor?

This is a homework problem. I am trying to pass an object through a constructor to create a new object with double the values. The object is an int array with upper and lower indices and a size. I'm not sure if I just change the values of the private variables to * 2 or if I'm missing something completely.

Here's my class header:

const int SIZE = 100;

class intArray{
private:
    int iArray[SIZE];
    int arrLower, arrUpper;
    int size;

public:
    intArray();
    intArray(int size);
    intArray(int lower, int upper);
    intArray(intArray& input);
    int high();
    int low();
    int& operator[] (int size)             {  return iArray[size];  }

};

and all I have for the constructor so far:

intArray::intArray(intArray& input){

    input.iArray[size] *= 2;
    input.size *= 2;
    input.arrUpper *= 2;
    input.arrLower *= 2;

}

which I am not even sure is correct.

intArray::intArray(intArray& input){

    input.iArray[size] *= 2;
    input.size *= 2;
    input.arrUpper *= 2;
    input.arrLower *= 2;

}

This is incorrect.

Since you prefix everything with input. , you are modifying the original which is not what you want. Just copy all the old values, but multiplied by two

intArray::intArray(intArray& input){

    for(int i = 0; i < size; i++) {
        iArray[i] = input.iArray[i] * 2; //set this array to 2x the input array
    }
    size = input.size * 2;
    arrUpper = input.arrUpper * 2;
    arrLower = input.arrLower * 2;

}

While this will sort of work, the array is the same size, but size is multiplied by two, which is just wrong. I'm not sure exactly what your class is actually supposed to be doing, but I can tell you it doesn't really make sense. What are arrUpper , and arrLower ? If you answer, I can help you some more.

Here is one that makes a little more sense, but I still don't know what some of your variables mean.

intArray::intArray(intArray& input){
    size = input.size; //keep this the same
    for(int i = 0; i < size; i++) {
        iArray[i] = input.iArray[i] * 2; //set this array to 2x the input array
    }
    arrUpper = input.arrUpper; //what is this even?
    arrLower = input.arrLower;

}

If you must keep the passed object intact, then pass it by const& . You should then modify the data members of the current object, not the parameter (and if you try it anyway, you'll receive errors).

intArray::intArray(intArray const& input) {
    for (int i = 0; i < size; i++) {
        iArray[i] = 2 * input.iArray[i];
    }

    size = 2 * input.size;
    arrUpper = 2 * input.arrUpper;
    input.arrLower = 2 * input.arrLower;
}

It seems like you have your copy constructor already defined (intArray::intArray(intArray&)). What you want to do in this is to perform a deep copy in your copy constructor of the array elements.

intArray::intArray(intArray& input)
{

    for(int i=input.low(); i<=input.hig(); ++i)
    {
        iArray[i] = input.iArray[i]*2;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM