1--Can operators be virtual?
2--What is the difference between an operator= returning a refernce Vs a value? 12 1238
"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
"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
"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
"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
"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.
"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.
"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
"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
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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 somewhere that only the
operators that need to modify theri lvalue should be members... what are
those...
|
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;
|
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 <? Do I do an
IF (a.letters < b.letters){
return true
}
|
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 isn't
completely unconstrained, since the bitwise operators seem to
presume a base-2 machine.
|
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)
{
| |
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 look like
and what are its arguments for?
|
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{
|
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).
|
by: Ranganath |
last post by:
Why is throw keyword considered as an operator?
|
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 these operators must be nonstatic member
functions, but why this ensure their first operands will...
|
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 usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
| |
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
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.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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 launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
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 into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
| |
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |