Creating and overloading new binary operators.

This is a discussion on Creating and overloading new binary operators. within the C++ Programming forums, part of the General Programming Boards category; In C++ can I define any character to be a binary operator? I know I can overload operators such as ...

  1. #1
    Registered User
    Join Date
    Feb 2002
    Posts
    5

    Smile Creating and overloading new binary operators.

    In C++ can I define any character to be a binary operator? I know I can overload operators such as +, -, *, /, %, &&,..etc, but can I defined the character 'A' (for instance) to be an operator, then overload it? For example, let x and y be integers, I would like to do something like
    z = x A y;

  2. #2
    Registered User rmullen3's Avatar
    Join Date
    Nov 2001
    Posts
    330
    "error C2833: 'operator A' is not a recognized operator or type"

    No.

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    Hmm, I suppose you could #define the operator into whatever you want. But I don't really see the use in it.
    Code:
    Point Point :: operator+ ( Point p )
    {
    // Overload the operator
    .
    .
    // Define a substitution for the + symbol
    #define A +
    .
    .
    // Use the substitution
    z = x A y;
    -Prelude
    My best code is written with the delete key.

  4. #4
    Registered User
    Join Date
    Feb 2002
    Posts
    5
    Originally posted by rmullen3
    "error C2833: 'operator A' is not a recognized operator or type"

    No.
    Is there no way around this? I got the same error too. I would think this idea might come up often enough there would be some way to do it in C++, or is this idea in general a bad for programming languages? I would really like to create a new character operators as opposed to using a function command to operate on my operands.

  5. #5
    Registered User rmullen3's Avatar
    Join Date
    Nov 2001
    Posts
    330
    I really don't see the point...

    But read Prelude's response. You can use #definitions if you really want it.

  6. #6
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    You cannot create new operators to overload in c++.
    You can only overload the already present operators. There are some that you should never overload too and some that you cannot overload.

    you cannot overload....

    .
    .*
    ::
    ?:
    new
    delete
    sizeof
    typeid
    static_cast
    dynamic_cast
    const_cast
    reinterpret_cast

    you can overload.....

    operator new
    operator new[]
    operator delete
    operator delete[]
    +
    -
    *
    /
    %
    ^
    &
    |
    ~
    !
    =
    <
    >
    +=
    -=
    *=
    /=
    %=
    ^=
    &=
    |=
    <<
    >>
    <<=
    >>=
    ==
    !=
    <=
    >=
    &&
    ||
    ++ pre and post
    -- pre and post
    ,
    ->*
    ->
    ()
    []

    operators you can but should never overload

    &&
    ||
    ,
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  7. #7
    Registered User
    Join Date
    Feb 2002
    Posts
    5
    Here is essentially what i want to do...
    Create a new operator 'd' which acts on two positive integer operands.

    It will simulate rolling dice. So if I wanted to roll 2 six-sided dice and put the random result into a new variable I would use a statement
    z = 2 d 6;

    as opposed to writing a function called roll( int x, int y ) that would return the desired random number.

    Is this just a bad idea? Should I just use the function format? I was mostly just interested if this could be done.

    I didn't quite understand the format given above using the #define directive, any chance you could clarify it for me a bit further? Would I need to use this code inside a class or could I do this above my main program?

  8. #8
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    Create a new operator 'd' which
    what did you not understand about....
    You cannot create new operators to overload in c++.
    You can only overload the already present operators.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Text RPG - Overloading Operators
    By legit in forum Game Programming
    Replies: 9
    Last Post: 11-25-2008, 01:47 PM
  2. strange linking error with operator overloading
    By George2 in forum C++ Programming
    Replies: 7
    Last Post: 07-03-2006, 08:32 AM
  3. operator overloading and dynamic memory program
    By jlmac2001 in forum C++ Programming
    Replies: 3
    Last Post: 04-07-2003, 12:51 AM
  4. Operater overloading - (const char + CString)
    By auth in forum C++ Programming
    Replies: 14
    Last Post: 10-24-2002, 10:22 AM
  5. Full operator overloading in BCB5
    By Mario in forum C++ Programming
    Replies: 5
    Last Post: 06-01-2002, 03:15 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21