Thread: Overloading unary operators using pointers

  1. #16
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Yes, so you can see that it also applies when overloading binary operators: the current object (i.e., *this) is the left hand operand when overloading as a member function.
    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

  2. #17
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    It would be legal as a non-member if the left-hand (the first) parameter was a reference to dummy_class. That is,

    void operator+(dummy_class& lhs, dummy_class* test); // Fine
    void operator+(dummy_class* lhs, dummy_class* test); // Not fine

    Also note that operator + should return a temporary of its left-hand side type. So it should be

    dummy_class operator+(dummy_class& lhs, dummy_class* test);
    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.

  3. #18
    Registered User
    Join Date
    Aug 2009
    Posts
    140
    Quote Originally Posted by Elysia View Post
    It would be legal as a non-member if the left-hand (the first) parameter was a reference to dummy_class. That is,

    void operator+(dummy_class& lhs, dummy_class* test); // Fine
    void operator+(dummy_class* lhs, dummy_class* test); // Not fine

    Also note that operator + should return a temporary of its left-hand side type. So it should be

    dummy_class operator+(dummy_class& lhs, dummy_class* test);
    Is that because addition of two pointers is not legal?

  4. #19
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    No, it's because it's a built-in operator. And you may not overload those.
    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.

  5. #20
    Registered User
    Join Date
    Aug 2009
    Posts
    140
    But addition of two pointers is not an operation which is defined?

  6. #21
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    True, pointer addition is not defined. Yet, it's still not allowed.
    Dumb of me to call it a built-in operator.
    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.

  7. #22
    Registered User
    Join Date
    Aug 2009
    Posts
    140
    Great, so I was correct it #18. Thanks for pointing it out.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 01-22-2002, 12:25 PM
  2. About overloading operators.
    By Dual-Catfish in forum C++ Programming
    Replies: 2
    Last Post: 12-29-2001, 07:12 PM
  3. Overloading operators...
    By Unregistered in forum C++ Programming
    Replies: 4
    Last Post: 11-21-2001, 08:24 PM
  4. Overloading Operators
    By Raven Arkadon in forum C++ Programming
    Replies: 1
    Last Post: 10-24-2001, 10:24 AM
  5. Overloading Operators
    By Strahan in forum C++ Programming
    Replies: 4
    Last Post: 09-07-2001, 11:39 AM