How do you use operator+()? I'm confused!

This is a discussion on How do you use operator+()? I'm confused! within the C++ Programming forums, part of the General Programming Boards category; I'm not sure how to use operator+(). Can you use it on just regular old integers or does it have ...

  1. #1
    Shadow12345
    Guest

    How do you use operator+()? I'm confused!

    I'm not sure how to use operator+(). Can you use it on just regular old integers or does it have to be done with classes?

  2. #2
    Registered User
    Join Date
    Feb 2002
    Posts
    57
    In follow-up:
    Code:
         // vector add
         const CVector operator+(const CVector &vec) const
         {
              return CVector(x + vec.x, y + vec.y, z + vec.z);
         }
    
         // vector add (opposite of negation)
         const CVector operator+() const
         {    
              return CVector(*this);
         }
    I don't get why the operator+() is there for..?
    I understand the first operator+(bla), but why is the second there, what does it do, when does it get called, and how does it work?!?!

    Thanks

  3. #3
    Shadow12345
    Guest
    focus more on Tazar's queston please.

  4. #4
    Peace
    Join Date
    Aug 2001
    Posts
    1,510
    Thats just using function overloading (and obviously; Operator overloading). In essence, if the function is called without parameters, the second function is executed. If called with [parameters], the first function is executed.

    The second function returns the object while the first returns the two added CVectors.

    >>Can you use it on just regular old integers or does it have to be done with classes?

    It can do anything you want. You're overloading it for your own use after all. The point is to keep it intuitive though...
    "There's always another way"
    -lightatdawn (lightatdawn.cprogramming.com)

  5. #5
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    the answer to your question is in the comment. Its the opposite of negation.
    say your object was an int 5 and you applied the negation operator
    int int::operator -()
    then the return would be -5.
    operator +() does just the opposite.
    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

  6. #6
    Registered User
    Join Date
    Aug 2001
    Posts
    101
    Tazar_Azar wrote:
    I don't get why the operator+() is there for..?
    I understand the first operator+(bla), but why is the second there, what does it do, when does it get called, and how does it work?!?!
    The first one is the binary operator+ which gets called when you place it between two operands:
    Code:
    v1 + v2; // same as: v1.operator+(v2);
    The second one is the unary operator+ which gets called if you place it before a single operand:
    Code:
    +v1; // same as: v1.operator+();
    - lmov

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Confused with array size
    By desmond5 in forum C Programming
    Replies: 4
    Last Post: 12-04-2007, 05:14 PM
  2. New to C++ and confused by boolean and ifs and else
    By jconner in forum C++ Programming
    Replies: 10
    Last Post: 08-02-2006, 04:29 AM
  3. why wont this compile?!? :confused:
    By jdude in forum C++ Programming
    Replies: 5
    Last Post: 11-25-2004, 01:13 AM
  4. confused.. in selecting my line of deapth
    By jawwadalam in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 05-04-2003, 02:21 PM
  5. Extern Question, really confused
    By SourceCode in forum C Programming
    Replies: 10
    Last Post: 03-26-2003, 11:11 PM

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