Overloading operator=

Jan 31, 2009 at 8:37pm
I am a bit confused with overloading operator=. Here is my function call, not working of course...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
Main.cpp
Polynomial test1;
Polynomial test2;
.
.
.
test2 = test1 * test1;


Polynomial.cpp
--------------
Polynomial* Polynomial::operator=(Polynomial& P){
        this._degree = P.getDegree();
	this._coefficients = P.getCoefficients();
	return this;
}

Jan 31, 2009 at 9:04pm
1. If an operator overload is supposed to return this, it should return it as a reference, not a pointer.
2. Polynomial::operator=() should be able to access Polynomial::_degree, so there's no need to call getDegree(). The same goes for _coefficients.
3. P should be a 'const Polynomial &'.
Last edited on Jan 31, 2009 at 9:04pm
Jan 31, 2009 at 11:22pm
4. this is a pointer - so this->x not this.x
Feb 1, 2009 at 12:26am
We assume that you already have operator* working, too.
Feb 1, 2009 at 12:38am
Damn. I knew C# was going to overwrite something. I totally ignored that dot.
Topic archived. No new replies allowed.