473,598 Members | 3,369 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

is operator= not polimorphic (virtual)?


#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::operator= (const A & src)
{
printf("A= called\n");
return * this;
}

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

A & B::operator= (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::operator=, 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
h.********@nosp am.gmx.de
Remove the nospam to get my real address
Oct 8 '05 #1
15 1881
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::operator=, 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::operator=(co nst A&). This produces the
output you observe.

What exactly do you want to do?

Markus

Oct 8 '05 #2
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::operator=(co nst 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).

Oct 8 '05 #3
"Heiner" <no*****@t-online.de> schrieb im Newsbeitrag
news:pa******** *************** ****@t-online.de...

#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::operator= (const A & src)
{
printf("A= called\n");
return * this;
}

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

A & B::operator= (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::operator=, 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
Oct 8 '05 #4
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::operator=). 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::operator= (const A & src)
{
printf("A= called, ");
return * this;
}

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

B & B::operator= (const B & src)
{
printf("B.3= called, ");
operator=((cons t 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
h.********@nosp am.gmx.de
Remove the nospam to get my real address
Oct 8 '05 #5
> 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.
Oct 8 '05 #6
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::operator=). 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::operator= (const A & src)
{
printf("A= called, ");
return * this;
}

A & B::operator= (const A & src)
{
printf("B.1= called, ");
A::operator=(sr c);
const B * srcB = dynamic_cast<co nst 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::operator= (const B & src)
{
printf("B.3= called, ");
operator=((cons t 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.

Oct 8 '05 #7
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::operator= 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
h.********@nosp am.gmx.de
Remove the nospam to get my real address
Oct 8 '05 #8
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 misunderstandin g of C++ default method
generation. If a class X has no X::operator=(co nst X &), the compiler
creates one. That's what happend here. With both operator= defined, my
example worked (see other posts, please)
Heiner

--

Heiner
h.********@nosp am.gmx.de
Remove the nospam to get my real address
Oct 8 '05 #9
> 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.
Oct 8 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
2432
by: Emanuel Ziegler | last post by:
Hello, I want to do some mathematics with functions. In my case the function classes are very complex, but this simple example has the same problems. To allow calculations that begin with a double, I have to define a global operator+ (or -*/) and special function classes. --- source begins here ---
2
4123
by: psane | last post by:
I have the following code #include <iostream> #include <string> using namespace std; class superclass { public: superclass () {}
12
1781
by: c++novice | last post by:
1--Can operators be virtual? 2--What is the difference between an operator= returning a refernce Vs a value?
17
3729
by: N4M | last post by:
Dear, Suppose I have a Protocol class, in which I need also an assignment operator =(). class B { ..... virtual B& operator=(const B& rb) =0; }; Now in some derived class D: public B, how would I proceed with operator=? Do I need to supply 2 operators:
0
1668
by: A. W. Dunstan | last post by:
I'm porting some code to Visual C++ and have run into a problem - the compiler won't use a user-written cast operator. The code uses an envelope-letter approach to passing (potentially) large pieces of data around, and requires that certain methods return an Envelope with a specific kind of Letter as it's content. I have a cast operator that converts from what I've got to what should be returned, but it seems that the compiler only...
8
17747
by: Floogle | last post by:
how do i create a virtual == operator. I've tried the following but it's incorrect... class Interface { ... public: virtual bool operator==(const Interface& rhs)const=0;
2
3079
by: www.brook | last post by:
I want to enfore the implementation of the operator overloading eg class Interface_ { public: virtual void operator = (const Interface &other)=0; virtual void operator +=(const Interface &other)=0; } but in the sub class
13
3949
by: JD | last post by:
Hi, My associate has written a copy constructor for a class. Now I need to add an operator = to the class. Is there a way to do it without change her code (copy constructor) at all? Your help is much appreciated. JD
6
4725
by: Rob McDonald | last post by:
I would like to force all the classes in my hierarchy to implement the << operator for testing purposes. My base class is a pure virtual class. I started out by working with operator overloading in the derived class. I have been trying to use what I learned there to create an appropriate virtual class to force overloading. class Base{
0
7902
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
8050
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8265
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
5850
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5438
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
3898
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2412
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1504
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1250
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.