Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Old 05-03-2005, 03:27 PM   #1 (permalink)
brad_galloway
Registered User
 
brad_galloway's Avatar
 
Join Date: Feb 2005
Location: Western KY
Posts: 24
brad_galloway is on a distinguished road
Operator Overloading for Enumerated types

Does anyone know how to overload operators for enumerated types? Such as the operator>>?
brad_galloway is offline   Reply With Quote
Old 05-03-2005, 04:17 PM   #2 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
Valmont is on a distinguished road
Yes, all you need to do is to pass an identifier of the enum type:
Code:
#include <iostream>

using namespace std;

enum MyEnum
{
  NIL, ONE, TWO, THREE
};

ostream& operator<<(ostream& os, const MyEnum& me)
{
  if(me == ONE)
  return os<<1;
  //etcetera
}

int main()
{
  MyEnum theEnum;
  cout<<ONE<<endl;

  cin.get();
  return 0;
}
__________________
Valmont is offline   Reply With Quote
Old 05-05-2005, 03:54 PM   #3 (permalink)
brad_galloway
Registered User
 
brad_galloway's Avatar
 
Join Date: Feb 2005
Location: Western KY
Posts: 24
brad_galloway is on a distinguished road
What about operator>>?

What about overloading operator>>?

My code is this:
Code:
#include<iostream>
using namespace std;

enum station {A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P};
istream& operator>>(istream& is, station& letter){
    is >> letter;
    if(letter == "A")
        return A;
    if(letter == "B")
        return B; 
    if(letter == "C")
        return C; 
    if(letter == "D")
        return D; 
    if(letter == "E")
        return E; 
    if(letter == "F")
        return F; 
    if(letter == "G")
        return G;
    if(letter == "H")
        return H; 
    if(letter == "I")
        return I; 
    if(letter == "J")
        return J; 
    if(letter == "K")
        return K; 
    if(letter == "L")
        return L;                   
    if(letter == "M")
        return M;
    if(letter == "N")
        return N; 
    if(letter == "O")
        return O; 
    if(letter == "P")
        return P; 
 }
It returns an error saying 'ISO C++ forbids comparison between pointer and integer'
'could not convert 'A' to 'std::istream&''
brad_galloway is offline   Reply With Quote
Old 05-05-2005, 06:51 PM   #4 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
Valmont is on a distinguished road
First rule #1:
For operator >> and << always return the stream at the end:
Code:
istream& / ostream& operator<</>>(istream/ostream& iostr, T& t)
{
   //code
   return iostr;
}
Now for your extraction operator:
Are you sure what you are doing? What are you trying to achieve? Something is fishy about overloading an extraction operator for the enum type... do you see why?

As for your answer:
What means this:
Code:
#include <iostream>
#include <ios>

using namespace std;

enum AnEnum {A, B, C, D};

int main()
{
  AnEnum MyEnum;
  //What???
  cin>>MyEnum;
  
  //Optional. Depends on IDE: prevents "console flashing".
  cin.get();
  return 0;
}
And this?
Code:
  //Come again?
  if(MyEnum == "Hi")
  {
      //Whatever.
  }
With tons of fantasy, I maybe could find a use for overloading the extraction operator for enum types. But you may ask yourself how sane that is .
Many employers threaten to fire programmers if they come up with such code. You have been warned.
__________________
Valmont is offline   Reply With Quote
Old 05-06-2005, 05:21 AM   #5 (permalink)
brad_galloway
Registered User
 
brad_galloway's Avatar
 
Join Date: Feb 2005
Location: Western KY
Posts: 24
brad_galloway is on a distinguished road
I might be a little green

Why is insane to overload the operator>>?

Is that just for enum types or overloading it for anything?
brad_galloway is offline   Reply With Quote
Old 05-06-2005, 07:12 AM   #6 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
Valmont is on a distinguished road
I want you to look again at the two code snippets I gave you. Then I ask you once again:
- do you think that is sane code?
If it is, why Brad? May you can teach me for the occasion .
- do you think fellow coders will understand what you're up to?

You're intelligent enough. Tell me what you think Brad.
__________________
Valmont is offline   Reply With Quote
Old 05-06-2005, 10:26 AM   #7 (permalink)
brad_galloway
Registered User
 
brad_galloway's Avatar
 
Join Date: Feb 2005
Location: Western KY
Posts: 24
brad_galloway is on a distinguished road
With all the stupid questions I have asked I don't see how you can say I'm intellegent. But thank you. And thanks for all the help you have provided. You have helped me revamp my entire coding style.

I guess I should have explained why I was trying to do it. The problem was to a subway router problem in which I had 16 stations identified by letters A-P. I was trying to allow the user to input two letters and then getting the program to solve the route and out put the path of stations they need to take. Although I could write a different function to do so. There are other ways than overloading the operator>>.

I'm still in the habit of trying to break out of my text-book way of thinking and becoming a more flexible programmer.
brad_galloway is offline   Reply With Quote
Old 05-06-2005, 10:59 AM   #8 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
Valmont is on a distinguished road
Well, just think of overloaded operators as conveniance functions. So find an easy (canonical) solution first, then see of operator overloading makes the code neater. Otherwise don't bother.
__________________
Valmont is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
? About data types... slashdot Standard C, C++ 2 04-15-2005 09:57 AM
Detecting binary image types with PHP . . possible? metazai PHP 6 02-16-2005 10:35 AM
Different string types in C: char pointer, char array, etc. Sleep Standard C, C++ 0 01-23-2005 08:40 PM
return types falsepride Standard C, C++ 19 01-04-2005 12:00 PM


All times are GMT -8. The time now is 07:41 AM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting