Overloading unary operators using pointers

This is a discussion on Overloading unary operators using pointers within the C++ Programming forums, part of the General Programming Boards category; Yes, so you can see that it also applies when overloading binary operators: the current object (i.e., *this) is the ...

  1. #16
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,444
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  2. #17
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,449
    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をこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,449
    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をこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,449
    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.

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 01-22-2002, 11:25 AM
  2. About overloading operators.
    By Dual-Catfish in forum C++ Programming
    Replies: 2
    Last Post: 12-29-2001, 06:12 PM
  3. Overloading operators...
    By Unregistered in forum C++ Programming
    Replies: 4
    Last Post: 11-21-2001, 07: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

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