473,503 Members | 1,783 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to properly overload the == operator?

1 New Member
I am trying to overload the == operator for a String class

Expand|Select|Wrap|Line Numbers
  1. bool String::&operator==(const char data[])
  2.         {
  3.  
  4.             for(int i=0;string[i]!='\0';i++)
  5.             {
  6.                 if(string[i]!=data[i]){return false;}
  7.             }
  8.             if(data[i+1]='\0'){return true;}
  9.             return false;
  10.         }
  11.  
running this:{
String mstr;
mstr="Hello World";
if(mstr=="Hello world"){...}
} gives me a bunch of errors. Can anyone help?
Nov 17 '12 #1
1 1551
weaknessforcats
9,208 Recognized Expert Moderator Expert
The equality operator== will return a bool. Things are equal or they're not.

The first operator is the object on the left of the operator. The second object is the one on the right of the operator.

Arguments should be by reference to avoid making a copy.

You must write code that compares the two objects. his does not mean comparing every data member but you must execute whatever logic is required to compare the left object with the right object.

The operator== may or may not be a member function.
You may have several operator== functions if your object can be compared to to other objects of different types.
Nov 18 '12 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

1
2038
by: Matthew Monopole | last post by:
Since in integer we can use either a = -b and a = b - c shouldn't it when we overload operator there'd be two way to overload the minus operator? if so, what's the prototypes? class...
4
1885
by: Chiller | last post by:
Ok, thanks to some good assistance/advice from people in this group I've been able to further develop my Distance class. Since previous posts I've refined my code to accept the unit measurement...
5
2111
by: Teddy | last post by:
Hello all consider the class Date declaretion below: class Date { public: Date(); Date(int year, int month, int day); Date(const string&); int getYear() const;
25
2792
by: Steve Richter | last post by:
is it possible to overload the << operator in c# similar to the way it is done in c++ ? MyTable table = new MyTable( ) ; MyTableRow row = new MyTableRow( ) ; row << new MyTableCell( "cell1 text...
2
1063
by: vcmkrishnan.techie | last post by:
Hi all, In managed C++, can I overload the operator to access data members in the following way Create object objTime; seconds = obj; or seconds = obj;
6
12449
by: Lighter | last post by:
Big Problem! How to overload operator delete? According to C++ standard, "A deallocation function can have more than one parameter."(see 3.7.3.2); however, I don't know how to use an overloaded...
3
1861
by: ek | last post by:
In the class below I overload the "()" operator. When reading an element in an int array "()" is therefore used. But why can't I still use "" when writing something like a = 4; class MyArray {...
2
1788
by: Mahain | last post by:
How we overload '=' operator using friend function in c++. if not why ????????????????????
1
1522
by: Maximus | last post by:
I am trying to find a way to overload operator new of a class to use a specialzied memory manager. In my design multiple memory managers exist. I need to pass the instance of a memory manager into...
2
1745
by: Markjan | last post by:
I have a problem with classes and structures in classes (C++) I have to overload operator . class Data { public: class Proxy { //for overload operator Data& _a; int...
0
7202
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,...
0
7280
Oralloy
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,...
0
5578
agi2029
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,...
0
4672
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...
0
3167
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...
0
3154
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1512
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 ...
1
736
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
380
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.