P
Przemek
Dear All,
I would like to know why the overriden operator = is not calles
properly by the function test in the following code. I really do not
understand it.
Thank you for any kind of help,
Przemyslaw
#include <iostream>
#include <cmath>
using namespace std;
class A {
public:
double a;
double b;
double c;
public:
A(const double& m_a, const double& m_b, const double& m_c)
{a=m_a; b=m_b; c=m_c;}
virtual ~A() {}
virtual A& operator = (const A&);
};
class B : public A {
public:
double d;
public:
B(const double& m_a, const double& m_b, const double& m_c, const
double& m_d) : A(m_a, m_b, m_c), d(m_d){}
~B() {}
B& operator = (const B&);
};
A& A:perator = (const A& m_A)
{
a = m_A.a;
b = m_A.b;
c = m_A.c;
std::cout << "base operator called " << std::endl;
return *this;
}
B& B:perator = (const B& m_B)
{
if(this!=&m_B)
(A&)(*this) = (A&)m_B;
a = m_B.a;
b = m_B.b;
c = m_B.c;
d = 12.4;
std::cout << "derived operator called " << std::endl;
return *this;
}
void test(A& a, const A& b) {a = b;}
int main()
{
A a(1, 2, 3);
B b(1, 2, 3, 4), c(0, 1, 2, 3);
test(b, c);
std::cout << a.a << " " << a.b << " " << a.c << std::endl;
std::cout << b.a << " " << b.b << " " << b.c << " " << b.d <<
std::endl;
std::cout << c.a << " " << c.b << " " << c.c << " " << c.d <<
std::endl;
return 0;
}
I would like to know why the overriden operator = is not calles
properly by the function test in the following code. I really do not
understand it.
Thank you for any kind of help,
Przemyslaw
#include <iostream>
#include <cmath>
using namespace std;
class A {
public:
double a;
double b;
double c;
public:
A(const double& m_a, const double& m_b, const double& m_c)
{a=m_a; b=m_b; c=m_c;}
virtual ~A() {}
virtual A& operator = (const A&);
};
class B : public A {
public:
double d;
public:
B(const double& m_a, const double& m_b, const double& m_c, const
double& m_d) : A(m_a, m_b, m_c), d(m_d){}
~B() {}
B& operator = (const B&);
};
A& A:perator = (const A& m_A)
{
a = m_A.a;
b = m_A.b;
c = m_A.c;
std::cout << "base operator called " << std::endl;
return *this;
}
B& B:perator = (const B& m_B)
{
if(this!=&m_B)
(A&)(*this) = (A&)m_B;
a = m_B.a;
b = m_B.b;
c = m_B.c;
d = 12.4;
std::cout << "derived operator called " << std::endl;
return *this;
}
void test(A& a, const A& b) {a = b;}
int main()
{
A a(1, 2, 3);
B b(1, 2, 3, 4), c(0, 1, 2, 3);
test(b, c);
std::cout << a.a << " " << a.b << " " << a.c << std::endl;
std::cout << b.a << " " << b.b << " " << b.c << " " << b.d <<
std::endl;
std::cout << c.a << " " << c.b << " " << c.c << " " << c.d <<
std::endl;
return 0;
}