Thread: Operator overload question

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    135

    Operator overload question

    I know that for + and - you use the following code;
    Code:
    complexClass complexClass::operator+ (complexClass a){}
    But can I use similar codes for *, /, and ^? Are they the same?
    Last edited by 843; 03-13-2011 at 11:52 AM.

  2. #2
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    733
    Yes.
    Pass by const reference:
    Code:
    complexClass complexClass::operator+ (const complexClass& a){}

  3. #3
    Registered User
    Join Date
    Oct 2010
    Posts
    135
    Why do I need const and to pass by reference? Why don't the + and - operators need to?

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by kmdv View Post
    Yes.
    Pass by const reference:
    Code:
    complexClass complexClass::operator+ (const complexClass& a){}
    no that's not right, if you use the one-parameter member function version then it should also be a const function.
    However, for the operators that return a result by value, e.g. +, -, *, /, |, ^, & etc, you should use the two-parameter friend, static or non-member version.
    e.g.
    Code:
    friend complexClass complexClass::operator+ (const complexClass &a, const complexClass &b)
    {
        complexClass c;
        // ... blah blah ...
        return c;
    }
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by 843 View Post
    Why do I need const and to pass by reference? Why don't the + and - operators need to?
    All operators should. And the why is because it's more efficient. You don't need to make copies of the objects all the time.
    And the const simply stops you from accidentally modifying the objects you are trying to work with.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Registered User
    Join Date
    Oct 2010
    Posts
    135
    I see now. Thanks for all the help!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Question bout my work
    By SirTalksAlots in forum C Programming
    Replies: 4
    Last Post: 07-18-2010, 03:23 PM
  2. A question about a question
    By hausburn in forum C++ Programming
    Replies: 3
    Last Post: 04-25-2010, 05:24 AM
  3. Alice....
    By Lurker in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 06-20-2005, 02:51 PM
  4. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  5. Question, question!
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-24-2001, 01:47 AM