?
Solved

Operator 0verloading

Posted on 2003-12-08
3
Medium Priority
?
270 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
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.

800 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