473,564 Members | 2,758 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

2 questions--virtual operators and operator=

1--Can operators be virtual?
2--What is the difference between an operator= returning a refernce Vs a value?
Jul 22 '05 #1
12 1775

"c++novice" <be************ **@yahoo.com> wrote in message
news:34******** *************** ***@posting.goo gle.com...
1--Can operators be virtual?
Yes.
2--What is the difference between an operator= returning a refernce Vs a

value?

Same as the difference between returning a reference or a value with any
other function.

john
Jul 22 '05 #2

"c++novice" <be************ **@yahoo.com> wrote in message
news:34******** *************** ***@posting.goo gle.com...
1--Can operators be virtual?
Yes.
2--What is the difference between an operator= returning a refernce Vs a

value?

Same as the difference between returning a reference or a value with any
other function.

john
Jul 22 '05 #3

"John Harrison" <jo************ *@hotmail.com> wrote in message
news:c4******** *****@ID-196037.news.uni-berlin.de...

"c++novice" <be************ **@yahoo.com> wrote in message
news:34******** *************** ***@posting.goo gle.com...
1--Can operators be virtual?


Yes.
2--What is the difference between an operator= returning a refernce Vs a

value?

Same as the difference between returning a reference or a value with any
other function.


Maybe that wasn't the most helpful answer. Here's a short program, run it
and see the result, then change the operator= return type to a value and run
it again, see what difference it makes.

#include <iostream>

struct X
{
X& operator=(const X& rhs)
{
x = rhs.x;
return *this;
}
int x;
};

int main()
{
X a, b, c;
a.x = 1;
b.x = 2;
c.x = 3;
(a = b) = c;
std::cout << a.x << '\n';
}

But this needs quite an advanced understanding of C++. All you really need
to know is returning a reference is the right thing to do.

john
Jul 22 '05 #4

"John Harrison" <jo************ *@hotmail.com> wrote in message
news:c4******** *****@ID-196037.news.uni-berlin.de...

"c++novice" <be************ **@yahoo.com> wrote in message
news:34******** *************** ***@posting.goo gle.com...
1--Can operators be virtual?


Yes.
2--What is the difference between an operator= returning a refernce Vs a

value?

Same as the difference between returning a reference or a value with any
other function.


Maybe that wasn't the most helpful answer. Here's a short program, run it
and see the result, then change the operator= return type to a value and run
it again, see what difference it makes.

#include <iostream>

struct X
{
X& operator=(const X& rhs)
{
x = rhs.x;
return *this;
}
int x;
};

int main()
{
X a, b, c;
a.x = 1;
b.x = 2;
c.x = 3;
(a = b) = c;
std::cout << a.x << '\n';
}

But this needs quite an advanced understanding of C++. All you really need
to know is returning a reference is the right thing to do.

john
Jul 22 '05 #5
"John Harrison" <jo************ *@hotmail.com> wrote in message news:<c4******* ******@ID-196037.news.uni-berlin.de>...
"c++novice" <be************ **@yahoo.com> wrote in message
news:34******** *************** ***@posting.goo gle.com...
1--Can operators be virtual?


Yes.

john


perhaps an example would be more helpful.

I am confused, I tried to write some code just to understand and see
the behavior. can some body explain me.
#include <iostream>

using namespace std;

class A {
public:
A() {
this->Foo();
}
virtual void Foo() {
cout << "A::Foo()" << endl;
}
virtual A& operator=(const A &rhs){
cout << "A& operator=(const A&)" << endl;
if(this == &rhs)
return *this;
return *this;
}
// virtual A &operator++( );
};

class B : public A {
public:
B() {
this->Foo();
}
virtual void Foo() {
cout << "B::Foo()" << endl;
}
virtual B& operator=(const B &rhs){
cout << "B& operator=(const B&)" << endl;
if(this == &rhs)
return *this;
return *this;
}
// virtual B &operator++( );
};

int main(int, char**)
{
A objA;
B objB;
objA = objB;

B anotherobjB;
anotherobjB = objA;
return 0;
}
how can we call op= of class B?
but op= if not declared, generated by compiler for every class then
why virtual it.
Jul 22 '05 #6
"John Harrison" <jo************ *@hotmail.com> wrote in message news:<c4******* ******@ID-196037.news.uni-berlin.de>...
"c++novice" <be************ **@yahoo.com> wrote in message
news:34******** *************** ***@posting.goo gle.com...
1--Can operators be virtual?


Yes.

john


perhaps an example would be more helpful.

I am confused, I tried to write some code just to understand and see
the behavior. can some body explain me.
#include <iostream>

using namespace std;

class A {
public:
A() {
this->Foo();
}
virtual void Foo() {
cout << "A::Foo()" << endl;
}
virtual A& operator=(const A &rhs){
cout << "A& operator=(const A&)" << endl;
if(this == &rhs)
return *this;
return *this;
}
// virtual A &operator++( );
};

class B : public A {
public:
B() {
this->Foo();
}
virtual void Foo() {
cout << "B::Foo()" << endl;
}
virtual B& operator=(const B &rhs){
cout << "B& operator=(const B&)" << endl;
if(this == &rhs)
return *this;
return *this;
}
// virtual B &operator++( );
};

int main(int, char**)
{
A objA;
B objB;
objA = objB;

B anotherobjB;
anotherobjB = objA;
return 0;
}
how can we call op= of class B?
but op= if not declared, generated by compiler for every class then
why virtual it.
Jul 22 '05 #7

"c++novice" <be************ **@yahoo.com> wrote in message
news:34******** *************** ***@posting.goo gle.com...
"John Harrison" <jo************ *@hotmail.com> wrote in message news:<c4******* ******@ID-196037.news.uni-berlin.de>...
"c++novice" <be************ **@yahoo.com> wrote in message
news:34******** *************** ***@posting.goo gle.com...
1--Can operators be virtual?


Yes.

john


perhaps an example would be more helpful.

I am confused, I tried to write some code just to understand and see
the behavior. can some body explain me.
#include <iostream>

using namespace std;

class A {
public:
A() {
this->Foo();
}
virtual void Foo() {
cout << "A::Foo()" << endl;
}
virtual A& operator=(const A &rhs){
cout << "A& operator=(const A&)" << endl;
if(this == &rhs)
return *this;
return *this;
}
// virtual A &operator++( );
};

class B : public A {
public:
B() {
this->Foo();
}
virtual void Foo() {
cout << "B::Foo()" << endl;
}
virtual B& operator=(const B &rhs){
cout << "B& operator=(const B&)" << endl;
if(this == &rhs)
return *this;
return *this;
}
// virtual B &operator++( );
};

int main(int, char**)
{
A objA;
B objB;
objA = objB;

B anotherobjB;
anotherobjB = objA;
return 0;
}
how can we call op= of class B?


You can't with that code. You have a A of the right hand side, but your
operator= for B takes B on the right hand side. You cannot convert an A to a
B. If you want operator= of B to be called when you have an A on the right
hand side then you have to declare like this

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

virtual has got nothing to do with it.
but op= if not declared, generated by compiler for every class then
why virtual it.


Why indeed? Why do you want a virtual operator=, what are you trying to
achieve?

You can certainly declare a virtual operator=, and it will behave exactly
like any other virtual function. But it isn't much use.

Here's some code that proves virtual operator= works.

#include <iostream>
using namespace std;

class A
{
public:
virtual A& operator=(int x) { cout << "A::operator=\n "; return *this; }
};

class B : public A
{
public:
virtual A& operator=(int x) { cout << "B::operator=\n "; return *this; }
};

int main()
{
A* a_ptr = new B();
*a_ptr = 1;
}

This code prints B::operator= but if you remove the virtuals it prints
A::operator=, which shows that virtual operator= is just like any other
virtual function.

I think your problem is that you are expecting virtual to do something that
it doesn't. Whatever it is that you are after I don't think virtual
operator= is the way to go.

If this hasn't helped then post again (perhaps in a new thread) and explain
what it is that you are trying to achieve. Someone will show you the right
way to do it.

john
Jul 22 '05 #8

"c++novice" <be************ **@yahoo.com> wrote in message
news:34******** *************** ***@posting.goo gle.com...
"John Harrison" <jo************ *@hotmail.com> wrote in message news:<c4******* ******@ID-196037.news.uni-berlin.de>...
"c++novice" <be************ **@yahoo.com> wrote in message
news:34******** *************** ***@posting.goo gle.com...
1--Can operators be virtual?


Yes.

john


perhaps an example would be more helpful.

I am confused, I tried to write some code just to understand and see
the behavior. can some body explain me.
#include <iostream>

using namespace std;

class A {
public:
A() {
this->Foo();
}
virtual void Foo() {
cout << "A::Foo()" << endl;
}
virtual A& operator=(const A &rhs){
cout << "A& operator=(const A&)" << endl;
if(this == &rhs)
return *this;
return *this;
}
// virtual A &operator++( );
};

class B : public A {
public:
B() {
this->Foo();
}
virtual void Foo() {
cout << "B::Foo()" << endl;
}
virtual B& operator=(const B &rhs){
cout << "B& operator=(const B&)" << endl;
if(this == &rhs)
return *this;
return *this;
}
// virtual B &operator++( );
};

int main(int, char**)
{
A objA;
B objB;
objA = objB;

B anotherobjB;
anotherobjB = objA;
return 0;
}
how can we call op= of class B?


You can't with that code. You have a A of the right hand side, but your
operator= for B takes B on the right hand side. You cannot convert an A to a
B. If you want operator= of B to be called when you have an A on the right
hand side then you have to declare like this

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

virtual has got nothing to do with it.
but op= if not declared, generated by compiler for every class then
why virtual it.


Why indeed? Why do you want a virtual operator=, what are you trying to
achieve?

You can certainly declare a virtual operator=, and it will behave exactly
like any other virtual function. But it isn't much use.

Here's some code that proves virtual operator= works.

#include <iostream>
using namespace std;

class A
{
public:
virtual A& operator=(int x) { cout << "A::operator=\n "; return *this; }
};

class B : public A
{
public:
virtual A& operator=(int x) { cout << "B::operator=\n "; return *this; }
};

int main()
{
A* a_ptr = new B();
*a_ptr = 1;
}

This code prints B::operator= but if you remove the virtuals it prints
A::operator=, which shows that virtual operator= is just like any other
virtual function.

I think your problem is that you are expecting virtual to do something that
it doesn't. Whatever it is that you are after I don't think virtual
operator= is the way to go.

If this hasn't helped then post again (perhaps in a new thread) and explain
what it is that you are trying to achieve. Someone will show you the right
way to do it.

john
Jul 22 '05 #9
Might want to take a look at http://www.parashift.com/c++-faq-lite
#include <iostream>

using namespace std;

class A {
public:
A() {
this->Foo();
This is not needed, the this-> is implictly added. And you might want
to read Might want to take a look at
http://www.parashift.com/c++-faq-lite ,
10.7 in the FAQ about the dangers of using this in the constructor (no
danger in this case though).
}
virtual void Foo() {
cout << "A::Foo()" << endl;
}
virtual A& operator=(const A &rhs){
cout << "A& operator=(const A&)" << endl;
if(this == &rhs)
return *this;
return *this;

Why bother with a selfassignment check if you don't do anything with
the result?
}
// virtual A &operator++( );
};

class B : public A {
public:
B() {
this->Foo();
}
virtual void Foo() { cout << "B::Foo()" << endl;
}
virtual B& operator=(const B &rhs){
cout << "B& operator=(const B&)" << endl;
if(this == &rhs)
return *this;
return *this;
} // virtual B &operator++( );
};

int main(int, char**)
please use int main () if you are not planning on using the command
line args {
A objA;
B objB;
objA = objB;
if you ever want to do this it is better to write
A* objA = new B;
Do not forget to call delete when you don't need objA anymore or it
will result in memory loss
or write
B objB;
A* objA = &objB;
The danger is here that you can call delete on objA releasing the
memory that objB points to. Meaning that if you try to use objB from
that point on your program should crash.

Doing this will result in the virtual functions you have overridden to
be accessed.
if you write
B objB;
A* objA = &objB;
objA->Foo();
it will put to the screen B::Foo()

Another point is that doing
A objA;
B objB;
objA = objB;
will result in slicing. Which means losing anything that the derived
class added to the base class in the form of datamembers and
functions.


B anotherobjB;
anotherobjB = objA;
This line should have generated an error. The compiler should complain
that B doesn't have any operator= overloads that accept a right side
of class A.
Class B might be a class A but class A is not a class B. You need to
explicitly specify a return 0;
}
how can we call op= of class B?
but op= if not declared, generated by compiler for every class then
why virtual it.


You need to explictly define
virtual B& operator=(const A &rhs)
{
// do stuff
}

if that isn't the answer on your question please explain what you want
more verbose please.
Jul 22 '05 #10

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

Similar topics

2
3572
by: Marcin Vorbrodt | last post by:
I see a lot of source code where some operators are defined as class members, and some as friends, or just outside the class. Can someone please explain what the general rule of thumb is? I understand that operators like == or != can/should be definced outside of the class in order to be symetric. What about all the other ones? I read...
3
2031
by: Alex Vinokur | last post by:
Member operators operator>>() and operator<<() in a program below work fine, but look strange. Is it possible to define member operators operator>>() and operator<<() that work fine and look fine? // --------- foo.cpp --------- #include <iostream> using namespace std;
20
1795
by: KL | last post by:
I am working on a school assignment, so please don't tell me the solution. I just want some direction. I am supposed to overload the >, <, ==, !=, >=, and <= operators using bool. I am having a bit of a problem in seeing what needs to happen here. I am just not sure how I do this. Will the overloading function recognize a < and a usual...
2
3454
by: Steve Summit | last post by:
-----BEGIN PGP SIGNED MESSAGE----- It's often explained that the reason for some of the imprecision in C's definition is so that C can be implemented on different kinds of machines -- say, those with 2's complement, 1's complement, or sign-magnitude arithmetic. But the followup remark is sometimes also made that the choice of arithmetic...
3
1298
by: Egbert Nierop \(MVP for IIS\) | last post by:
Example... This would make that i can assign a ULONG to a CComBSTR2 class while the body of the code performs necessary conversion. CComBSTR2& operator=(ULONG ulong) {
11
4745
by: Jim Michaels | last post by:
friend fraction& operator+=(const fraction& rhs); fraction.h(64) Error: error: 'fraction& operator+=(const fraction&)' must take exactly two arguments I practically pulled this out of a C++ book (except for the "friend"). can someone explain why GCC is giving me problems here? for a += or similar operator, what does a proper declaration...
14
2791
by: Jess | last post by:
Hi, I read about operator overloading and have a question regarding "operator->()". If I have two classes like this: struct A{ void f(); }; struct B{
19
2281
by: Jess | last post by:
Hello, After seeing some examples about operator overloading, I'm still a bit confused about the general syntax. The following is what I think, not sure whether it's correct. 1. For a unary operator that's a member of a class, its form is usually "operatorP()" (where P is the operator's name).
18
3207
by: Ranganath | last post by:
Why is throw keyword considered as an operator?
8
2960
by: Wayne Shu | last post by:
Hi everyone, I am reading B.S. 's TC++PL (special edition). When I read chapter 11 Operator Overloading, I have two questions. 1. In subsection 11.2.2 paragraph 1, B.S. wrote "In particular, operator =, operator, operator(), and operator-must be nonstatic member function; this ensures that their first operands will be lvalues". I know that...
0
7665
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7583
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...
0
7888
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7950
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...
0
6255
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5484
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...
0
5213
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...
0
3643
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...
0
3626
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.