Hello everyone,
I was reading my OOP lectures, getting ready for the course test and I came through the implementation of op= and I have a question.
There the thumb rules for the implementation of the op=(free old memory, allocate new memory, work with new memory, etc), as shown in here:
1 2 3 4 5 6 7 8
String& String::operator=(const String& rhs){ //string's op=
if (this==&rhs) return *this;
len = rhs.len;
delete[] s;
s = newchar[len +1];
strcpy(s, rhs.s);
return *this;
}
After seeing this example I was wondering why do I need relocate memory for class which I know it's pointers point to a constant-size array, like this example shows:
1 2 3 4 5 6 7
//how do I implement an op= for this class:
class A{
int* a;
double* b;
public: A() { a=newint[3];
b=newdouble [5]; }
}
SO, the question is why do I need to relocate new memory and work with it(I emailed my lecturer and she told me if I won't do it(relocate memory and work with if) it may cause other problems rather than MEMORY LEAK).