1

Possible Duplicate:
Operator overloading

What I want to do is overloading an operator which adds a constant with a polynomial. Here is the class definition.

class polynomial
{
public:
    polynomial();// default constructor
    polynomial(double*,int);//  parameterized constructor
    polynomial(polynomial*);// copy constructor
    ~polynomial();//destructor
    polynomial* const operator+(polynomial*);
    polynomial* const operator+(double);
    friend polynomial* const operator+(double,polynomial);
    polynomial* operator-(polynomial*);
    polynomial* operator-(const double);
    polynomial* operator*(polynomial*);
    polynomial* polynomial::operator*(const double);
    friend ostream& operator<<(ostream&,polynomial*);
    friend double evaluate(polynomial*,double);
    friend double* extract(polynomial*);
    polynomial* operator =(polynomial*);
    int degree;
private:
    double* polyNum;
};

Because we can not access into the definition of build-in class to add the operator we want as a member function. We have no choice but a non-member function. But when I am trying to do so, it comes up with an error that "..'operator +' must have at least one formal parameter of class type.."

    friend polynomial* const operator+(double,polynomial*);

However, when I try to pass a object instead of the pointer of it, another problem that when it return from other function it will call the destructor automatically and erase all the data pointed by the pointer inside the object.

    polynomial* const operator+(double constant,polynomial p)
{
->  return p+constant;
}

  • p {degree=3 polyNum=0x00de8188 {1.0000000000000000} } polynomial

polynomial* const operator+(double constant,polynomial p)
{
    return p+constant;
->
}

  • p {degree=3 polyNum=0x00de8188 {-2.6569842580370804e+303} } polynomial
Community
  • 1
  • 1
Rui Zheng
  • 189
  • 6
  • 1
    Please read and understand this question and its answers: [Operator overloading](http://stackoverflow.com/questions/4421706/operator-overloading) – Robᵩ Nov 28 '12 at 03:56
  • Firstly, your second parameter should have reference type, not pointer type. Secondly, your operator should return a temporary object `polynomial`, not a pointer. But again, read the answers linked by Rob. – AnT stands with Russia Nov 28 '12 at 03:59

2 Answers2

0

You seem to be confused about the need for pointers. (Hint: there is no need for pointers in your class.)

To answer your specific question, your operator+ functions should look like this:

class polynomial {
public:
  polynomial& operator +=(double d) {
     polynum[0] += d; // or whatever
     return *this;
  }
  polynomial& operator+=(const polynomial& p) {
    for( ... ) {
      polynum[i] += p.polynum[i]; // or whatever
    }
    return *this;
  }

  // other stuff goes here, too
  ...
};

polynomial operator+(polynomial p, double d) { return p += d; }

polynomial operator+(double d, polynomial p) { return p += d; }

polynomial operator+(polynomial l, const polynomial& r) { return l += r; }

Typical usage would be:

int main () {
  polynomial p, q, r;
  double d;
  ... fill in p, q, and d ...
  q = p + d;
  r = d + p;
  p = q + r;
}
Robᵩ
  • 163,533
  • 20
  • 239
  • 308
  • Thank you very much. But my instructor ask me to use practice how to use dynamic arrays. So I have to use a pointer instead of the real array...And I find the problem just now. It is missing of the copy constructor. BTW: the way you write code inline is so beautiful :P – Rui Zheng Nov 28 '12 at 04:19
  • I can understand your instructor's need for you to declare `double* polyNum;` While I disagree with his teaching, I understand it. What I am saying is that you shouldn't have, for example, `polynomial* operator =(polynomial*);`. The use pointers in *that* declaration is seriously wrong. – Robᵩ Nov 28 '12 at 14:37
  • Which one is the one seriously wrong? The pointer as parameter or the one as return value? Or both... I think if I replace any one of them will make the code less efficient because I have to pass the entire object rather than a address everytime the copy constrictor is used. – Rui Zheng Nov 29 '12 at 21:32
0

The only copy constructor I have before is

polynomial(polynomial*);
// copy constructor work with pointer

It makes that when I pass an object rather than a pointer as an argument into function

 polynomial* const operator+(double constant,polynomial p)
{
    return p+constant;
}

the local variable just simply copy the value from the external variable, including the value of p.polynum. As a result, there are two pointer pointing at one same array. When the function is done and ready to return, it calls the destructor to destroy the local variable p, which destroy the array of p.polynum by the way. That is why the value inside the array that external pointer pointing to is erased. And that is why we need a real copy constructor for classes which contain pointers.

Solution:

Add a extra copy constructor as:

polynomial(polynomial*);
// copy constructor2 work with object.
Rui Zheng
  • 189
  • 6