I took this example from http://publib.boulder.ibm.com/infoce...ef/cplr318.htm
Code:
// This example illustrates overloading the plus (+) operator.
#include <iostream>
using namespace std;
class complx
{
double real,
imag;
public:
complx( double real = 0., double imag = 0.); // constructor
complx operator+(const complx&) const; // operator+()
};
// define constructor
complx::complx( double r, double i )
{
real = r; imag = i;
}
// define overloaded + (plus) operator
complx complx::operator+ (const complx& c) const
{
complx result;
result.real = (this->real + c.real);
result.imag = (this->imag + c.imag);
return result;
}
int main()
{
complx x(4,4);
complx y(6,6);
complx z = x + y; // calls complx::operator+()
}
- What does it do when assigning the values to variables inside the constructor? complx( double real = 0., double imag = 0.);
- What does it do when having const infront of an argument? (const complx&)
- What does it do having const after a signature? (const complx&) const; by the way is this still considered part of the signature?
- Is there such a thing as returning a const?
I felt anyone of these questions would be unworthy of an entire thread so I put them in one. Hopefully I'm not breaking the rules for this.