Solved

Operator 0verloading

Posted on 2003-12-08
3
264 Views
Last Modified: 2008-03-06
Having a thick morning today - its a monday!

Can you overload operators for enumerations eg:

enum enum1{ enum_list.....};
enum enum2{enum2_lsit.....};

bool operator<(enum1 lhs, enum2 rhs);

Or can you only overload where one of the operands is a class?
0
Comment
Question by:mattjsimps
3 Comments
 
LVL 44

Assisted Solution

by:Karl Heinz Kremer
Karl Heinz Kremer earned 50 total points
ID: 9895884
You can overload operators for enums. One example is listed in "The C++ Programming Language" (3rd edition) in 11.2.3 Operators and User-Defined Types.

Your example however would not work: You are comaring two different user defined types (enum1 and enum2). What you can do is:
bool operator<(enum1 lsh, enum1 rhs);

The compiler would not know how to convert your enum2 to an enum1 (or vice versa), so a comparison would not be possible. It's pretty much like comparing apples and oranges :-)

0
 
LVL 11

Accepted Solution

by:
bcladd earned 80 total points
ID: 9896068
khkremer-

You can overload operators for DIFFERENT enums, too. The syntax is exactly as in the post above (see sample program below). The semantics, the meaning of comparing different enums, that is up to the programmer to provide. In the sample code below the macintosh apple is the king of fruit, less than no form of orange. The valencia orang is the trash of fruit, better than no apple. Then the others are compared by their underlying representations. _I_ gave the two this meaning and it is comparing apples and oranges.

mattjsimps-

For programmer convenience if you provide comparisons such as operator<(apple, orange) you should, symetrically, provide operator<(orange, apple).

Sample program:
#include <iostream>

using namespace std;

enum apples { macintosh, delicious, spy, ida_red, NUMBEROFAPPLES };
enum oranges { navel, blood, valencia, NUMBEROFORANGES };

string appleName[] = { "macintosh", "delicious", "spy", "ida_red", "" };
string orangeName[] = { "navel", "blood", "valencia", "" };

bool operator< ( const apples a, const oranges b)
{
  if (a == macintosh) return false;
  if (b == valencia) return true;
  return (int)a > int(b);
}

int main(int argc, char **argv)
{
  for(apples a = macintosh; a <= ida_red; ++a) {
    for (oranges o = navel; o <= valencia; ++o) {
      if (a < o)
        cout << appleName[a] << " < " << orangeName[o] << endl;
      else
        cout << appleName[a] << " NOT < " << orangeName[o] << endl;
    }
  }
 
} /** End of main **/
0
 
LVL 1

Author Comment

by:mattjsimps
ID: 9896348
well spotted. I do in fact want to compare two values of the same type. Told you it was a thick morning!

I am going to increase points to 130 and split 80:50 in bcladd's favour, since the answer answered what i asked.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now