473,385 Members | 1,766 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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 1755

"c++novice" <be**************@yahoo.com> wrote in message
news:34**************************@posting.google.c om...
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.google.c om...
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.google.c om...
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.google.c om...
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.google.c om...
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.google.c om...
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.google.c om...
"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.google.c om...
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.google.c om...
"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.google.c om...
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
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 #11
ve*********@hotmail.com (velthuijsen) wrote in message news:<e5**************************@posting.google. com>...
Might want to take a look at http://www.parashift.com/c++-faq-lite


Thank u and u too john.
I am clear now on this.
Jul 22 '05 #12
ve*********@hotmail.com (velthuijsen) wrote in message news:<e5**************************@posting.google. com>...
Might want to take a look at http://www.parashift.com/c++-faq-lite


Thank u and u too john.
I am clear now on this.
Jul 22 '05 #13

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

Similar topics

2
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...
3
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...
20
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...
2
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...
3
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
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++...
14
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
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...
18
by: Ranganath | last post by:
Why is throw keyword considered as an operator?
8
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,...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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,...
0
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...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.