Thread: How do you define operators for custom types?

  1. #1
    Registered User
    Join Date
    Feb 2016
    Posts
    6

    How do you define operators for custom types?

    I tried googling on how to do this but I couldn't find any tutorials or instructions on how to define an operator for a custom type. I'm not sure if it can be done or not, but I've defined operators before in scala and haskell, operators as in "+" and "==". If anyone can point me to instructions on how to do this with C I would appreciate it.

    This is my custom typedef:

    Code:
    typedef enum token_types{                 
        BEGIN, END, READ, WRITE, ID,
        INTLITERAL, LPAREN, RPAREN,
        SEMICOLON, COMMA, ASSIGNOP,
        PLUSOP, MINUSOP, SCANEOF
    } token;
    I need to define a comparison operator for these tokens. I'm using this to create a small compiler.

  2. #2
    Registered User
    Join Date
    Dec 2015
    Posts
    112
    Enums are integral constants. You can do what you are talking with an interface and implementation.

    You essentially define a data type, and create functions with the operations you want based on that type. You can separate the implementation from the interface so you can easily make changes without affecting the client's code.

    http://forge.huihoo.com/attachments/...e-software.pdf

  3. #3
    Registered User
    Join Date
    Feb 2016
    Posts
    6
    Thanks for the help, but apparently this isn't necessary. I just compared two of those types with the enums, and it seems I don't need to go around defining operators. I'm really new at C, this is like first time I've used it.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,403
    Quote Originally Posted by ArgusPMC
    I tried googling on how to do this but I couldn't find any tutorials or instructions on how to define an operator for a custom type. I'm not sure if it can be done or not, but I've defined operators before in scala and haskell, operators as in "+" and "==". If anyone can point me to instructions on how to do this with C I would appreciate it.
    C does not support operator overloading, but because operator overloading is just for syntactic sugar, you never need it, though it can be nice to have.

    Anyway, as CodeSlapper pointed out, the enumeration just lists a number of integer constants as a group in a loosely defined type, so you can use the same operators for int on them. Chances are though, you might want to use a switch.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Define custom comparison
    By Lagrange in forum C++ Programming
    Replies: 2
    Last Post: 11-15-2009, 08:43 AM
  2. how to define a custom entry point
    By Bleech in forum Windows Programming
    Replies: 10
    Last Post: 09-17-2006, 09:42 PM
  3. Custom variable types in C++
    By Aeroren in forum C++ Programming
    Replies: 2
    Last Post: 07-06-2005, 10:07 AM
  4. Clipboard and Custom Types
    By McClamm in forum C# Programming
    Replies: 1
    Last Post: 09-16-2004, 04:43 PM
  5. Custom File Types
    By Misled Hacker in forum Game Programming
    Replies: 6
    Last Post: 11-07-2002, 07:30 PM

Tags for this Thread