M
Mike
The copy_constructor is called when a function returns an
object (copys it out) right?
But in this program the operator+ function doesnt call it.
Can someone tell me why please ?
Thanks!!
----8<----8<----8<----8<----8<----
#include <iostream>
using namespace std;
class Complex {
protected:
float re, im;
public:
Complex(float re, float im) {
cout << "constructor\n";
this->re = re;
this->im = im;
}
Complex(const Complex& x) {
cout << "copy_constructor\n";
re = x.re;
im = x.im;
}
Complex& operator=(const Complex& x) {
if(this != &x) {
cout << "operator=\n";
re = x.re;
im = x.im;
}
return *this;
}
~Complex() {
cout << "destructor\n";
}
void Print(void) {
cout << "re: " << re << " im: " << im << endl;
}
friend Complex operator+ (const Complex& a, const Complex& b);
};
Complex operator+ (const Complex& a, const Complex& b) {
cout << "operator+\n";
Complex tempc(a.re + b.re, a.im + b.im);
return tempc; // <----- !!! THIS SHOULD CALL THE copy_constructor !!!
}
Complex test(Complex c) {
c.Print();
return c;
}
int main(void)
{
Complex c1(1.1, 2.2), c2(11.11, 22.22);
c1.Print();
c2.Print();
cout << "** start c1 = c1 + c2;\n";
c1 = c1 + c2;
cout << "** end c1 = c1 + c2;\n";
c1.Print();
c2.Print();
return 0;
}
/*
this is the program output:
constructor
constructor
re: 1.1 im: 2.2
re: 11.11 im: 22.22
** start c1 = c1 + c2; <-----+
operator+ |
constructor | the copy_constructor should
operator= | be called somewhere here ...
destructor |
** end c1 = c1 + c2; <-----+
re: 12.21 im: 24.42
re: 11.11 im: 22.22
destructor
destructor
*/
----8<----8<----8<----8<----8<----
object (copys it out) right?
But in this program the operator+ function doesnt call it.
Can someone tell me why please ?
Thanks!!
----8<----8<----8<----8<----8<----
#include <iostream>
using namespace std;
class Complex {
protected:
float re, im;
public:
Complex(float re, float im) {
cout << "constructor\n";
this->re = re;
this->im = im;
}
Complex(const Complex& x) {
cout << "copy_constructor\n";
re = x.re;
im = x.im;
}
Complex& operator=(const Complex& x) {
if(this != &x) {
cout << "operator=\n";
re = x.re;
im = x.im;
}
return *this;
}
~Complex() {
cout << "destructor\n";
}
void Print(void) {
cout << "re: " << re << " im: " << im << endl;
}
friend Complex operator+ (const Complex& a, const Complex& b);
};
Complex operator+ (const Complex& a, const Complex& b) {
cout << "operator+\n";
Complex tempc(a.re + b.re, a.im + b.im);
return tempc; // <----- !!! THIS SHOULD CALL THE copy_constructor !!!
}
Complex test(Complex c) {
c.Print();
return c;
}
int main(void)
{
Complex c1(1.1, 2.2), c2(11.11, 22.22);
c1.Print();
c2.Print();
cout << "** start c1 = c1 + c2;\n";
c1 = c1 + c2;
cout << "** end c1 = c1 + c2;\n";
c1.Print();
c2.Print();
return 0;
}
/*
this is the program output:
constructor
constructor
re: 1.1 im: 2.2
re: 11.11 im: 22.22
** start c1 = c1 + c2; <-----+
operator+ |
constructor | the copy_constructor should
operator= | be called somewhere here ...
destructor |
** end c1 = c1 + c2; <-----+
re: 12.21 im: 24.42
re: 11.11 im: 22.22
destructor
destructor
*/
----8<----8<----8<----8<----8<----