Two + operators

This is a discussion on Two + operators within the C++ Programming forums, part of the General Programming Boards category; Okay, I'm writing a class, and I want to know how I can have two different + operators in the ...

  1. #1
    Registered User
    Join Date
    Aug 2003
    Posts
    14

    Two + operators

    Okay, I'm writing a class, and I want to know how I can have two different + operators in the class.

    Is it even possible to do this?

    Thanks!

  2. #2
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571
    Yes, for different types of right-hand side arguments.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

  3. #3
    Registered User
    Join Date
    Aug 2003
    Posts
    14
    So as long as they have different parameters, you can do it?

  4. #4
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    Isn't there also a uranary + operator analgalous to the uranary - operator? I could be very wrong about this...
    FAQ

    "The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs." -- Joseph Weizenbaum.

    "If you cannot grok the overall structure of a program while taking a shower, you are not ready to code it." -- Richard Pattis.

  5. #5
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571
    ++ and -- you mean?
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

  6. #6
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    "unary"

    You mean a '+5' analogous to '-5' (for example)?
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  7. #7
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    Yeah, Zach knows what I'm thinking. Is that possible?
    FAQ

    "The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs." -- Joseph Weizenbaum.

    "If you cannot grok the overall structure of a program while taking a shower, you are not ready to code it." -- Richard Pattis.

  8. #8
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Yep.

    Code:
    class A
    {
    public:
      A operator+(); // Unary +: A x; +x;
      A operator+(A b); // Binary +: A x, y; x + y;
    };
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  9. #9
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    *fist pump*
    And I suppose other things would go on this line...
    FAQ

    "The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs." -- Joseph Weizenbaum.

    "If you cannot grok the overall structure of a program while taking a shower, you are not ready to code it." -- Richard Pattis.

  10. #10
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,399
    Originally posted by Zach L.
    Yep.

    Code:
    class A
    {
    public:
      A operator+(); // Unary +: A x; +x;
      A operator+(A b); // Binary +: A x, y; x + y;
    };
    Not that I don't have confidence in you, Zach, but I was almost sure that you were wrong. Then I checked and learned.

    Why is there a unary + operator? With the basic types, +x returns x. So why use +x at all?
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  11. #11
    Registered User glUser3f's Avatar
    Join Date
    Aug 2003
    Posts
    345
    +x = x if x is a number, however, c++ provides a way to overload + so you can use it to do whatever you want for your class, one of the features that I like in c++.

  12. #12
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Originally posted by pianorain
    Not that I don't have confidence in you, Zach, but I was almost sure that you were wrong. Then I checked and learned.
    Thats good. Someone keeping me honest.


    Why is there a unary + operator? With the basic types, +x returns x. So why use +x at all?
    I really can't see much use for it. If you're gonna use operators, they should logically work as you'd expect them (otherwise, use a named function), and I can't think of any logical place where +x != x. It was probably added to C++ since it is mathematically valid syntax.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Logical Operators in C++
    By Flecto in forum C++ Programming
    Replies: 4
    Last Post: 05-15-2009, 08:17 AM
  2. Default Class Operators
    By symbiote3 in forum C++ Programming
    Replies: 2
    Last Post: 06-13-2008, 02:44 PM
  3. Bolean Operators hurt my head. (Trouble understanding) :(
    By Funcoot in forum C++ Programming
    Replies: 3
    Last Post: 01-20-2008, 07:42 PM
  4. operators???
    By arjunajay in forum C++ Programming
    Replies: 11
    Last Post: 06-25-2005, 05:37 AM
  5. Operators
    By George in forum C++ Programming
    Replies: 3
    Last Post: 04-02-2003, 07:35 PM

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