Solved

Operator 0verloading

Posted on 2003-12-08
3
263 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
Comment Utility
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
Comment Utility
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
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

744 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

7 Experts available now in Live!

Get 1:1 Help Now