473,698 Members | 2,178 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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
12 1791
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*********@hot mail.com (velthuijsen) wrote in message news:<e5******* *************** ****@posting.go ogle.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*********@hot mail.com (velthuijsen) wrote in message news:<e5******* *************** ****@posting.go ogle.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
3577
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...
3
2042
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;
20
1833
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 }
2
3466
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.
3
1310
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
4756
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?
14
2802
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
2292
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).
18
3226
by: Ranganath | last post by:
Why is throw keyword considered as an operator?
8
2971
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...
0
8675
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, 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...
0
8604
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,...
0
9029
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8862
tracyyun
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...
1
6521
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5860
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();...
0
4619
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3050
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
2
2331
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.