2 questions--virtual operators and operator=

C

c++novice

1--Can operators be virtual?
2--What is the difference between an operator= returning a refernce Vs a value?
 
J

John Harrison

c++novice said:
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
 
J

John Harrison

John Harrison said:
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
 
C

c++novice

John Harrison said:
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.
 
J

John Harrison

c++novice said:
"John Harrison" <[email protected]> wrote in message

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::eek:perator=\n"; return *this; }
};

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

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

This code prints B::eek:perator= but if you remove the virtuals it prints
A::eek:perator=, 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
 
V

velthuijsen

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top