Problem with overriden operators

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::eek: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::eek: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;
}
 
R

Rolf Magnus

Przemek said:
Dear All,

I would like to know why the overriden operator = is not calles
properly by the function test in the following code.

There is no overriden operator= in your example. The operator= in B has
another signature than the one in A, therefore it doesn't override the
one in A.
 
B

Bob Hairgrove

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.

[snip]

As others have pointed out, the arguments for operator= differ, so
they are overloaded, but not overriden functions (or did I get this
backwards again?).

One way of implementing such a polymorphic assignment operator would
be to make the base class operator= the only publicly accessible one.
It would not need to be virtual because it will not be overridden. Do
any assignment to base class members there and have an additional
protected pure virtual "assign()" function which the base class'
operator= calls. Override the protected "assign()" function, but hide
the operator= in the derived classes (by declaring it as private but
do not provide a body for it).
 
B

Bob Hairgrove

On Tue, 22 Jun 2004 13:38:50 +0200, Bob Hairgrove

[snip]
protected pure virtual "assign()" function which the base class'
operator= calls.

Of course, it is more complicated than that because you would need to
control the types you are assigning from / to.

IOW, if you have A<--B and A<--C, how do both B=C and C=B work
correctly? Or how could that be prevented?
 
R

Rolf Magnus

Bob said:
On Tue, 22 Jun 2004 13:38:50 +0200, Bob Hairgrove

[snip]
protected pure virtual "assign()" function which the base class'
operator= calls.

Of course, it is more complicated than that because you would need to
control the types you are assigning from / to.

IOW, if you have A<--B and A<--C, how do both B=C and C=B work
correctly? Or how could that be prevented?

An object cannot change its type. The type will be the same from the
beginning to the end of its life time. Therefore, it's impossible in
C++ to use the assignment operator polymorphically in the way you (or
rather the OP) want.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,241
Members
46,831
Latest member
RusselWill

Latest Threads

Top