Overloaded assignment operator for class template objects

Pages: 12
In regards to:

1
2
          for (i = arrayLowerBound; i < arrayUpperBound; i++)
              arrayPtr[i] = rightArray.arrayPtr[i];


geeloso wrote:
Like I said, I already made all the adjustments related to the array indexing throughout my class' member functions.


If you were depending on your (now corrected operator[]) to automagically adjust the index in cases like the above, it doesn't, because it doesn't use your class's operator[].

If you did wish to use your operator[] here it would look like:

1
2
    for ( int i=arrayLowerBound; i<arrayUpperBound; ++i )
        (*this)[i] = rightArray[i] ; 


I wouldn't be at all surprised to learn you still had some code like this lurking about in your class somewhere.
@MiiNiPaa: as you requested, this is my modified overloaded assignment operator:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
template<class dataType>
const myArray<dataType>& myArray<dataType>::operator=(const myArray<dataType>& rightArray) 
{
     int i;
     int arraySize;
     
     arraySize = arrayUpperBound - arrayLowerBound;
    
     if(this != &rightArray)
     {
          delete [] arrayPtr;
          arrayLowerBound = rightArray.arrayLowerBound;
          arrayUpperBound = rightArray.arrayUpperBound;
          arrayPtr = new dataType[arraySize];
          assert(arrayPtr != NULL);
          for (i = 0; i < arraySize; i++)
              arrayPtr[i] = rightArray.arrayPtr[i];
     }
     return *this;
}



@cire: oh yeah, I'm not trying to use the overloaded array index operator in any of the class member functions' definitions. I know that the system-defined version would be used by the compiler.
Just a thought! Could the Dev C++ IDE I'm using be actually screwing up my logically correct code?! Althougth I must say it has served me well so far as I've written some great programs on it.

Any ideas on other better free IDEs I can try? I just can't see anything I'm doing wrong in this code!
Does anyone know other C++ programming sites/forums I can consult in order to have my Herculean problem resolved??
this is my modified overloaded assignment operator

Some minor comments:
- couldn't line 7 be moved inside the if(this .. guard?
- the assert at line 15 is pointless, unless you're using an archaic, pre-standard compiler (standard C++'s new operator doesn't return null when it fails, it raises an exception)
- you could also more the variable i and arraySize into tighter scopes (looks very C at the moment)
- and switch from i++ to ++i, if you're keen...

Andy
Last edited on
Does anyone know other C++ programming sites/forums I can consult in order to have my Herculean problem resolved??


If you posted the entire implementation of your rewritten myArray type (and how you're exercising it, if that has changed) you would have a much better chance of getting relevant advice.

I see nothing wrong with your operator= so the problem is (unless I missed something) somewhere else in your code.
@andywestken: Your comment about the in-built assert function I used is noted; I was just sticking to the format in my C++ text. However, the moves you suggested would not really impact code functionality; they would just somewhat make for code succinctness and aesthetics.

@cire: I concur with your observation about the flawlessness of my assignment operator function; I have looked it over countless times, making any required adjustments! Did you see the program run/output I showed on the first page of this thread? Please check it if you haven't; you'll see why I was thinking the assignment operator function was the culprit all along. Do you still need more info?
1
2
3
4
5
6
7
8
9
10
11
     arraySize = arrayUpperBound - arrayLowerBound; //your size
    
     if(this != &rightArray)
     {
          delete [] arrayPtr;
          arrayLowerBound = rightArray.arrayLowerBound;
          arrayUpperBound = rightArray.arrayUpperBound;
          arrayPtr = new dataType[arraySize]; //making it the same size as before
          assert(arrayPtr != NULL);
          for (i = 0; i < arraySize; i++) //looping on this size
              arrayPtr[i] = rightArray.arrayPtr[i];
suppose that you do a=b, where b has size 3 and a has size 42

> Could the Dev C++ IDE I'm using be actually screwing up my logically correct code?
Why do you look at the speck of sawdust in your brother's eye and pay no attention to the plank in your own eye?


PS: your `array' prefix is bothersome, especially considering that those are member variables.
Last edited on
geeloso wrote:
I concur with your observation about the flawlessness of my assignment operator function

I said I saw nothing wrong with it. However, I didn't test it; I just eyeballed it. I certainly wouldn't have described it as flawless.

ne555 wrote:
suppose that you do a=b, where b has size 3 and a has size 42

Didn't see that one! (Also not a fan of the naming strategy.)
code succinctness and aesthetics

- Restricting variables to as tight a scope as possible is not about aesthetics. It's about cutting down the chance of something untoward happening to them. It also allows the compiler to optimize better in some cases, as it's easier to spot when you're done with a variable. Not a big deal on the small scale, I guess.
- Favouring ++i over i++ is not about aesthetics (at least to me). In some circumstances there can be performance implications for post-increment; by consistently using pre-increment you won't inadvertently use post increment in the wrong place.

Before I wrote my last post I downloaded your code and give it a go, as I could see any reason why it would work for ints but not doubles (strings are a different matter; your test case runs fine for me.

But I had adjusted your operator= to this...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
template<class dataType>
const myArray<dataType>& myArray<dataType>::operator=(const myArray<dataType>& rightArray) 
{
     if(this != &rightArray)
     {
          delete [] arrayPtr;
          arrayLowerBound = rightArray.arrayLowerBound;
          arrayUpperBound = rightArray.arrayUpperBound;
          arraySize count = arrayUpperBound - arrayLowerBound;
          arrayPtr = new dataType[arraySize];
          for (int i = 0; i < arraySize; i++)
              arrayPtr[i] = rightArray.arrayPtr[i];
     }
     return *this;
}


So I inadvertently fixed the problem which ne555 just pointed out: that the array size was being calculated on the wrong (old) bounds.

When I reinstate your version of operator=, the test case crashes (I tweaked your test code to add values automatically rather than manually, see below).

Andy

PS Modified setArrayData()

1
2
3
4
5
6
7
8
9
10
11
12
13
14
template<class dataType>
void myArray<dataType>::setArrayData()
{
     if(arrayPtr != NULL)
     {
          int count = arrayUpperBound - arrayLowerBound;
          for (int i = 0; i < count; i++)
          {
              dataType value = dataType();
              genRandVal(value);
              arrayPtr[i] = value;
          }
     }
}


which uses (non-template function)

1
2
3
4
void genRandVal(double& value)
{
    value = (rand() % 100) / 3.0;
}


Plus added srand(time(0)); to start of program, as per usual.
Last edited on
Eureka!! Hurray!! Yeehaw!!! Are there other exclamatory outbursts of elation out there?! I'm happy to report to you guys that my code is now fully functional for all datatypes!! The root of the problem in my adjusted code was where I placed the arraySize = arrayUpperBound - arrayLowerBound statement. I moved it within the body of the IF block; it's amazing how I could have left it out marooned where it was! By the way, the assignment operator function I initially posted on the first page had this part right. I guess several pairs of eyes are better than one. I later saw that's the same thing andywestken did.

Thanks andywestken for test-running the code at your end; thanks ne555 for your acerbic critique; and thanks especially to cire and MiiNiPaa - you guys have been with me from the beginning!

Just relish this program output for strings:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39

Enter 5 data items for a myArray object: Monday Tuesday Wednesday Thursday Friday

Original list, list(5) = Monday Tuesday Wednesday Thursday Friday


Enter 11 data items for a myArray object: January February March April May June July August September October
November

Original myList, myList(2,13) = January February March April May June July August September October November


Enter 14 data items for a myArray object: Anna Beth Cindy Donna Emma Frieda Ginger Holly Isabella Janet Kira L
indsay Mariana Natasha

Original yourList, yourList(-5,9) = Anna Beth Cindy Donna Emma Frieda Ginger Holly Isabella Janet Kira Lindsay
 Mariana Natasha


Enter 9 data items for a myArray object: Mercury Venus Earth Mars Jupiter Saturn Uranus Neptune Pluto

Original hisList, hisList(-1,8) = Mercury Venus Earth Mars Jupiter Saturn Uranus Neptune Pluto



After assigning list to myList, myList(5) = Monday Tuesday Wednesday Thursday Friday

After assigning yourList to myList, myList(-5,9) = Anna Beth Cindy Donna Emma Frieda Ginger Holly Isabella Jan
et Kira Lindsay Mariana Natasha

After assigning hisList to list, list(-1,8) = Mercury Venus Earth Mars Jupiter Saturn Uranus Neptune Pluto

After assigning hisList to yourList, yourList(-1,8) = Mercury Venus Earth Mars Jupiter Saturn Uranus Neptune P
luto



Press any key to continue . . .



Please, don't start knocking me over Pluto not being a planet anymore. LOL! Thanks again to y'all.
Last edited on
Topic archived. No new replies allowed.
Pages: 12