Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > 2 questions--virtual operators and operator=

Reply
Thread Tools

2 questions--virtual operators and operator=

 
 
c++novice
Guest
Posts: n/a
 
      04-06-2004
1--Can operators be virtual?
2--What is the difference between an operator= returning a refernce Vs a value?
 
Reply With Quote
 
 
 
 
John Harrison
Guest
Posts: n/a
 
      04-06-2004

"c++novice" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) 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


 
Reply With Quote
 
 
 
 
John Harrison
Guest
Posts: n/a
 
      04-06-2004

"John Harrison" <(E-Mail Removed)> wrote in message
news:c4v1sk$2o3lmb$(E-Mail Removed)-berlin.de...
>
> "c++novice" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed) 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


 
Reply With Quote
 
c++novice
Guest
Posts: n/a
 
      04-07-2004
"John Harrison" <(E-Mail Removed)> wrote in message news:<c4v1sk$2o3lmb$(E-Mail Removed)-berlin.de>...
> "c++novice" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed) 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.
 
Reply With Quote
 
John Harrison
Guest
Posts: n/a
 
      04-08-2004

"c++novice" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) om...
> "John Harrison" <(E-Mail Removed)> wrote in message

news:<c4v1sk$2o3lmb$(E-Mail Removed)-berlin.de>...
> > "c++novice" <(E-Mail Removed)> wrote in message
> > news:(E-Mail Removed) 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:perator=\n"; return *this; }
};

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

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

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


 
Reply With Quote
 
velthuijsen
Guest
Posts: n/a
 
      04-08-2004
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.
 
Reply With Quote
 
c++novice
Guest
Posts: n/a
 
      04-08-2004
http://www.velocityreviews.com/forums/(E-Mail Removed) (velthuijsen) wrote in message news:<(E-Mail Removed). 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.
 
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
sql IN (and other special operators) and bind parameters Iain Barnett Ruby 0 02-02-2011 10:03 PM
Re: Any way to use boolean operators and control words in ThunderBird Ctrl+F and Ctrl+K search? Andreas Borutta Firefox 0 10-06-2010 10:23 PM
simple (?) question regarding the "&&" and "and" operators Philip Müller Ruby 4 04-18-2009 07:33 AM
perldb in emacs and shell * and ~ operators newsgroupie2003@yahoo.dk Perl Misc 0 08-15-2005 07:06 AM
Strings and Operators JamesW Perl 2 02-20-2004 02:34 PM



Advertisments