Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > is operator= not polimorphic (virtual)?

Reply
Thread Tools

is operator= not polimorphic (virtual)?

 
 
Heiner
Guest
Posts: n/a
 
      10-08-2005

#include <stdio.h>

class A
{
public:
virtual A & operator= (const A &);
virtual void test(const A &);
};

class B : public A
{
public:
virtual A & operator= (const A &);
virtual void test(const A &);
};

A & A:perator= (const A & src)
{
printf("A= called\n");
return * this;
}

void A::test(const A & src)
{
printf("A::test called\n");
}

A & B:perator= (const A & src)
{
printf("B= called\n");
return * this;
}

void B::test(const A & src)
{
printf("B::test called\n");
}

int main (int)
{
A a;
B b1, b2;
printf("b1 = a: "); b1 = a;
printf("b1 = b2: "); b1 = b2;
printf("b1.test(a): "); b1.test(a);
printf("b1.test(b2): "); b1.test(b2);
return 0;
}

I would have guessed, that b1 = b2 calls B:perator=, as b1.test(b2)
calls B::test. But what I get is:

7of9# gmake && ./test
g++ main.cpp -o test
b1 = a: B= called
b1 = b2: A= called
b1.test(a): B::test called
b1.test(b2): B::test called
7of9# g++ --version
g++ (GCC) 3.4.2 [FreeBSD] 20040728
Copyright (C) 2004 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

Is operator= not polymorphic?


Heiner
http://www.velocityreviews.com/forums/(E-Mail Removed)
Remove the nospam to get my real address


 
Reply With Quote
 
 
 
 
Markus Moll
Guest
Posts: n/a
 
      10-08-2005
Heiner wrote:

> #include <stdio.h>
>
> class A
> {
> public:
> virtual A & operator= (const A &);
> virtual void test(const A &);
> };
>
> class B : public A
> {
> public:
> virtual A & operator= (const A &);
> virtual void test(const A &);
> };

[...]
> I would have guessed, that b1 = b2 calls B:perator=, as b1.test(b2)
> calls B::test. But what I get is:

[...]
> b1 = a: B= called
> b1 = b2: A= called
> b1.test(a): B::test called
> b1.test(b2): B::test called

[...]
> Is operator= not polymorphic?


Yes, but...
There is more than one operator= involved, and that is your problem.
You do not provide a default assignment operator for class B, so the
compiler generates one with signature operator=(const B&);
This assignment operator works by assigning base classes and members
individually, thus invoking A:perator=(const A&). This produces the
output you observe.

What exactly do you want to do?

Markus

 
Reply With Quote
 
 
 
 
Heinz Ozwirk
Guest
Posts: n/a
 
      10-08-2005
"Heiner" <(E-Mail Removed)> schrieb im Newsbeitrag
news(E-Mail Removed)...
>
> #include <stdio.h>
>
> class A
> {
> public:
> virtual A & operator= (const A &);
> virtual void test(const A &);
> };
>
> class B : public A
> {
> public:
> virtual A & operator= (const A &);
> virtual void test(const A &);
> };
>
> A & A:perator= (const A & src)
> {
> printf("A= called\n");
> return * this;
> }
>
> void A::test(const A & src)
> {
> printf("A::test called\n");
> }
>
> A & B:perator= (const A & src)
> {
> printf("B= called\n");
> return * this;
> }
>
> void B::test(const A & src)
> {
> printf("B::test called\n");
> }
>
> int main (int)
> {
> A a;
> B b1, b2;
> printf("b1 = a: "); b1 = a;
> printf("b1 = b2: "); b1 = b2;
> printf("b1.test(a): "); b1.test(a);
> printf("b1.test(b2): "); b1.test(b2);
> return 0;
> }
>
> I would have guessed, that b1 = b2 calls B:perator=, as b1.test(b2)
> calls B::test. But what I get is:
>
> 7of9# gmake && ./test
> g++ main.cpp -o test
> b1 = a: B= called
> b1 = b2: A= called
> b1.test(a): B::test called
> b1.test(b2): B::test called
> 7of9# g++ --version
> g++ (GCC) 3.4.2 [FreeBSD] 20040728
> Copyright (C) 2004 Free Software Foundation, Inc.
> This is free software; see the source for copying conditions. There is NO
> warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR
> PURPOSE.
>
> Is operator= not polymorphic?


Polymorphism doesn't matter in this example. Polymorphism only matters when
functions are called through a pointer or reference to an object, not when
it is called directly for an object of known type.

Your guess is wrong because you forgot the compiler supplied assignment
operator, which is called for b1 = b2, and which in turn calls A's
(explicit) assignment operator, which prints the message.

HTH
Heinz


 
Reply With Quote
 
Rolf Magnus
Guest
Posts: n/a
 
      10-08-2005
Heiner wrote:

[...]

> int main (int)
> {
> A a;
> B b1, b2;
> printf("b1 = a: "); b1 = a;
> printf("b1 = b2: "); b1 = b2;


The above line calls the compiler generated B& B:perator=(const B&), which
in turn calls the base class's operator=.

> printf("b1.test(a): "); b1.test(a);
> printf("b1.test(b2): "); b1.test(b2);
> return 0;
> }


[...]

> Is operator= not polymorphic?


It is if you make it virtual (as you did).

 
Reply With Quote
 
Heiner
Guest
Posts: n/a
 
      10-08-2005
On Sat, 08 Oct 2005 13:04:34 +0200, Markus Moll wrote:

> What exactly do you want to do?


Thanks for both answers. I have a container class AC containing A
instances. This container can be cloned: it clones all A instances as well
(by calling A:perator=). Now there are derived classes: BC is
derived from AC and B from A. Therefore the container BC contains B
instances (and maybe A instances as well), which can be cloned. I have not
dublicated the clone code of AC, as I thought virtual operator= will do
the magic. Actually it didn't. So I changed my code to:

#include <stdio.h>

class A
{
public:
virtual A & operator= (const A &);
};

class B : public A
{
public:
virtual A & operator= (const A &);
virtual B & operator= (const B &);
};

A & A:perator= (const A & src)
{
printf("A= called, ");
return * this;
}

A & B:perator= (const A & src)
{
printf("B.1= called, ");
A:perator=(src);
const B * srcB = dynamic_cast<const B *>(& src);
if (srcB)
printf("B.2= called, ");
return * this;
}

B & B:perator= (const B & src)
{
printf("B.3= called, ");
operator=((const A &)src);
return * this;
}

int main (int)
{
A a;
B b1, b2;
printf("b1 = a: "); b1 = a;
printf("\nb1 = b2: "); b1 = b2;
A* a1 = & b1, *a2 = &b2;
printf("\n*a1 = *a2: "); *a1 = *a2;
printf("\n");
return 0;
}


which now gives:

7of9# gmake && ./test
g++ main.cpp -o test
b1 = a: B.1= called, A= called,
b1 = b2: B.3= called, B.1= called, A= called, B.2= called,
*a1 = *a2: B.1= called, A= called, B.2= called,

I think that is how it should be implemented.
Or is there a dynamic_cast free solution as well?


Heiner
(E-Mail Removed)
Remove the nospam to get my real address


 
Reply With Quote
 
Fraser Ross
Guest
Posts: n/a
 
      10-08-2005
> class B : public A
> {
> public:
> virtual A & operator= (const A &);
> virtual void test(const A &);
> };



Is there any reason at any time for writing an assignment operator that
return a reference to a class not of the type the function is within?

The example appears to show a compiler bug with BCB6.

Fraser.


 
Reply With Quote
 
Rolf Magnus
Guest
Posts: n/a
 
      10-08-2005
Heiner wrote:

> On Sat, 08 Oct 2005 13:04:34 +0200, Markus Moll wrote:
>
>> What exactly do you want to do?

>
> Thanks for both answers. I have a container class AC containing A
> instances.


Instances?

> This container can be cloned: it clones all A instances as well
> (by calling A:perator=). Now there are derived classes: BC is
> derived from AC and B from A. Therefore the container BC contains B
> instances (and maybe A instances as well), which can be cloned.


If it stores instances, no polymorphism is involved.

> I have not dublicated the clone code of AC, as I thought virtual operator=
> will do the magic. Actually it didn't.


An object cannot change its class during its life time. So the assignment
operator cannot transform an A object into a B object.

> So I changed my code to:
>
> #include <stdio.h>
>
> class A
> {
> public:
> virtual A & operator= (const A &);
> };
>
> class B : public A
> {
> public:
> virtual A & operator= (const A &);
> virtual B & operator= (const B &);
> };
>
> A & A:perator= (const A & src)
> {
> printf("A= called, ");
> return * this;
> }
>
> A & B:perator= (const A & src)
> {
> printf("B.1= called, ");
> A:perator=(src);
> const B * srcB = dynamic_cast<const B *>(& src);
> if (srcB)
> printf("B.2= called, ");
> return * this;
> }


So if the object provided on the right hand side is not a B, the assignment
operator does in fact nothing. So why have that operator at all? It would
be better to get an error if you try that instead of silently ignoring it.

> B & B:perator= (const B & src)
> {
> printf("B.3= called, ");
> operator=((const A &)src);
> return * this;
> }


> I think that is how it should be implemented.


Well, if you want several objects of different types in one container, you
shouldn't store instances but pointers to them in the container. Then, and
only then, can you use polymorphism.

 
Reply With Quote
 
Heiner
Guest
Posts: n/a
 
      10-08-2005
On Sat, 08 Oct 2005 16:29:01 +0200, Rolf Magnus wrote:

> Instances?

I use the term instance for any usage of a class. So A a creates an
instance and A* a = new A as well. If the term is wrong, sorry.

Maybe "pointer of reference to an instance" is more correctly

> Well, if you want several objects of different types in one container, you
> shouldn't store instances but pointers to them in the container. Then, and
> only then, can you use polymorphism.


That's what I wanted to discuss. My container AC contains pointers to
instances of A, and so do BC with B, while BC derives from AC and B from
A. A:perator= is virtual, so that the cloning code within AC should work
for BC instances as well.

I guess, I know now how to do it. Thanks again.

Heiner
(E-Mail Removed)
Remove the nospam to get my real address


 
Reply With Quote
 
Heiner
Guest
Posts: n/a
 
      10-08-2005
On Sat, 08 Oct 2005 13:13:53 +0100, Fraser Ross wrote:

>> class B : public A
>> {
>> public:
>> virtual A & operator= (const A &);
>> virtual void test(const A &);
>> };

>
>
> Is there any reason at any time for writing an assignment operator that
> return a reference to a class not of the type the function is within?


Yes, see the rest of the discussion. As B derives from A I wanted to
overwrite A's operator=. Therefore I have to use the same signature.

> The example appears to show a compiler bug with BCB6.


Actually it only showed my misunderstanding of C++ default method
generation. If a class X has no X:perator=(const X &), the compiler
creates one. That's what happend here. With both operator= defined, my
example worked (see other posts, please)


Heiner

--

Heiner
(E-Mail Removed)
Remove the nospam to get my real address


 
Reply With Quote
 
Fraser Ross
Guest
Posts: n/a
 
      10-08-2005
> Yes, see the rest of the discussion. As B derives from A I wanted to
> overwrite A's operator=. Therefore I have to use the same signature.


You can't overload with a different return type alone and therefore you
can override with a different return type alone. I think you have an
unwanted way of writing a return type for assignment operators. Noone
has given any reason for writing them as such yet.

Fraser.


 
Reply With Quote
 
 
 
Reply

Thread Tools

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
To be not, or not to be not? Ruby Freak Ruby 2 09-23-2008 08:04 AM
Why not 'foo = not f' instead of 'foo = (not f or 1) and 0'? Kristian Domke Python 11 01-23-2008 07:27 PM
'' is not a valid name. Make sure that it does not include invalid characters or punctuation and that it is not too long. rote ASP .Net 2 01-23-2008 03:07 PM
Cisco 3640 3620 3600 not detecting, not enabling, not working: NM-2FE2W Taki Soho Cisco 0 09-22-2004 07:28 AM
maintaining control with cookies (not strictly an ASP or even server side question. But not not either) Stephanie Stowe ASP General 2 04-07-2004 04:23 PM



Advertisments