C
c++novice
1--Can operators be virtual?
2--What is the difference between an operator= returning a refernce Vs a value?
2--What is the difference between an operator= returning a refernce Vs a value?
value?c++novice said:1--Can operators be virtual?
Yes.
2--What is the difference between an operator= returning a refernce Vs a
John Harrison said:value?
Same as the difference between returning a reference or a value with any
other function.
John Harrison said:Yes.
john
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?
but op= if not declared, generated by compiler for every class then
why virtual it.
#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.
Might want to take a look at http://www.parashift.com/c++-faq-lite
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.