Connecting Tech Pros Worldwide Help | Site Map

Problem with overriden operators

 
LinkBack Thread Tools Search this Thread
  #1  
Old July 22nd, 2005, 01:00 PM
Przemek
Guest
 
Posts: n/a
Default Problem with overriden operators

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::operator = (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::operator = (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;
}

  #2  
Old July 22nd, 2005, 01:00 PM
Rolf Magnus
Guest
 
Posts: n/a
Default Re: Problem with overriden operators

Przemek wrote:
[color=blue]
> Dear All,
>
> I would like to know why the overriden operator = is not calles
> properly by the function test in the following code.[/color]

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.

  #3  
Old July 22nd, 2005, 01:35 PM
Karl Heinz Buchegger
Guest
 
Posts: n/a
Default Re: Problem with overriden operators

Przemek wrote:[color=blue]
>
> 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.[/color]

Because for a virtual dispatch only the runtime type of the object
through which the call is made is relevant.

In[color=blue]
> void test(A& a, const A& b) {a = b;}[/color]

the compiler is searching for a function

operator=( constA& b );

and the only one it could find is in class A.
Since a is of type class B, which is derived from
class A, this one is taken.
The fact that b is in reality a class B object is
irrelevant when the correct function to call is
searched.

--
Karl Heinz Buchegger
kbuchegg@gascad.at
  #4  
Old July 22nd, 2005, 01:35 PM
Bob Hairgrove
Guest
 
Posts: n/a
Default Re: Problem with overriden operators

On 22 Jun 2004 03:26:56 -0700, przemyslaw.sliwa@gazeta.pl (Przemek)
wrote:
[color=blue]
>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.[/color]

[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).


--
Bob Hairgrove
NoSpamPlease@Home.com
  #5  
Old July 22nd, 2005, 01:35 PM
Bob Hairgrove
Guest
 
Posts: n/a
Default Re: Problem with overriden operators

On Tue, 22 Jun 2004 13:38:50 +0200, Bob Hairgrove
<wouldnt_you_like@to_know.com> wrote:

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

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?

--
Bob Hairgrove
NoSpamPlease@Home.com
  #6  
Old July 22nd, 2005, 02:05 PM
Rolf Magnus
Guest
 
Posts: n/a
Default Re: Problem with overriden operators

Bob Hairgrove wrote:
[color=blue]
> On Tue, 22 Jun 2004 13:38:50 +0200, Bob Hairgrove
> <wouldnt_you_like@to_know.com> wrote:
>
> [snip][color=green]
>>protected pure virtual "assign()" function which the base class'
>>operator= calls.[/color]
>
> 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?[/color]

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.

 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,840 network members.