Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Operator 0verloading

Posted on 2003-12-08
3
Medium Priority
?
271 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 44

Assisted Solution

by:Karl Heinz Kremer
Karl Heinz Kremer earned 200 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 320 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

Tech or Treat! - Giveaway

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

Question has a verified solution.

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

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
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.
Suggested Courses

604 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