c++ postfix / prefix operator overload as non-member function

thassan

I am writing my own array class as an exercise. Since, I read non-member functions are actually better in some ways than member functions. (Scott Meyers)

I am trying to write as many operator overloads as non-member functions as possible. The operator overloads + , - all work out fine as non-member functions.

my_array operator+(const my_array & left, const my_array & right);
my_array operator-(const my_array & operand);
my_array & operator++();  // prefix
my_array   operator++(int); //postfix, compiler puts a 0

However, the prefix/postfix operators as non-member functions give issues (they work fine if I use scope resolution and make them member functions)

I understand that not every operator overload can be member functions. But , I am having trouble as to why these two cannot be non-member functions. The error I get is:

: 'my_array& operator++()' must have an argument of class or enumerated type

Which basically can be solved if I make them member functions and allow a *this array obj to be passed along in the following format.

(*this).operator++();

But the whole thing is, I do not want to make them member functions in first place! So, is it that the pre/post fix operators cannot/should not be implemented as non-member function?

The reasoning I came up with is that, since postfix/prefix is unary operator they only have one argument (usually a *this). So, if I want the compiler to provide the *this pointer implicitly and call the overloads, they must be implemented as a member-function.

Is My reasoning correct? If not how do I implement this as a non-member function? Thanks for providing me with some insight.

WhozCraig

Perhaps I misunderstood, but if you're struggling with proper declaration of both operators, you can still do this with free operators like members. You do, however, need to pass the object as the first parameter by-reference. You're correct that as member functions they get their object for free via this. As a free function, you need to push it yourself.

#include <iostream>

struct my_array
{
    // your members here.
};

my_array& operator ++(my_array& obj)
{
    // access to members is through obj.member
    std::cout << "++obj called." << std::endl;
    return obj;
}

my_array operator ++(my_array& obj, int)
{
    my_array prev = obj;

    // modify obj, but return the previous state.        
    std::cout << "obj++ called." << std::endl;

    return prev;
}

int main(int argc, char *argv[])
{
    my_array obj;
    ++obj;
    obj++;
    return 0;
}

Output

++obj called.
obj++ called.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Templated operator overload resolution, member vs non-member function

From Dev

C++: Overload a member's constructor function with keyword operator

From Dev

Prefix and Postfix operator overloading in C#

From Dev

Prefix and Postfix operator overloading in C#

From Dev

Purpose of Dummy Parameter in Postfix Operator Overload? c++

From Dev

Relation between ostream insertion operator and its non-member overload

From Dev

Why can't I overload C++ conversion operators outside a class, as a non-member function?

From Dev

Increment a number by prefix and postfix operator

From Dev

Increment a number by prefix and postfix operator

From Dev

operator<< overload for a member type in a template class in C++

From Dev

operator<< overload for a member type in a template class in C++

From Dev

Operator overload: Member vs. non-member when only same type objects can be involved

From Dev

How to overload operator<< for a class member?

From Dev

How to properly overload postfix increment operator?

From Dev

Undefined Behavior of Postfix or Prefix Increment in Function Calls in C

From Dev

overload operator as generic function

From Dev

Calling a function in an operator overload?

From Dev

Private function for operator overload

From Dev

overload operator as generic function

From Dev

Overload copy assignment operator for a member struct of a non-type template struct

From Dev

How can I overload () operator as prefix?

From Dev

prefix and postfix operators c++

From Dev

C++ overload operator '>>'

From Dev

C ++ operator overload ++

From Dev

C++ Overload operator ==

From Dev

Overload operator in C++

From Dev

const call operator calling bound non-const member function

From Dev

Indexed access operator overload for a MutableList member in Kotlin

From Dev

iterator Overload Member Selection vs Indirection Operator

Related Related

  1. 1

    Templated operator overload resolution, member vs non-member function

  2. 2

    C++: Overload a member's constructor function with keyword operator

  3. 3

    Prefix and Postfix operator overloading in C#

  4. 4

    Prefix and Postfix operator overloading in C#

  5. 5

    Purpose of Dummy Parameter in Postfix Operator Overload? c++

  6. 6

    Relation between ostream insertion operator and its non-member overload

  7. 7

    Why can't I overload C++ conversion operators outside a class, as a non-member function?

  8. 8

    Increment a number by prefix and postfix operator

  9. 9

    Increment a number by prefix and postfix operator

  10. 10

    operator<< overload for a member type in a template class in C++

  11. 11

    operator<< overload for a member type in a template class in C++

  12. 12

    Operator overload: Member vs. non-member when only same type objects can be involved

  13. 13

    How to overload operator<< for a class member?

  14. 14

    How to properly overload postfix increment operator?

  15. 15

    Undefined Behavior of Postfix or Prefix Increment in Function Calls in C

  16. 16

    overload operator as generic function

  17. 17

    Calling a function in an operator overload?

  18. 18

    Private function for operator overload

  19. 19

    overload operator as generic function

  20. 20

    Overload copy assignment operator for a member struct of a non-type template struct

  21. 21

    How can I overload () operator as prefix?

  22. 22

    prefix and postfix operators c++

  23. 23

    C++ overload operator '>>'

  24. 24

    C ++ operator overload ++

  25. 25

    C++ Overload operator ==

  26. 26

    Overload operator in C++

  27. 27

    const call operator calling bound non-const member function

  28. 28

    Indexed access operator overload for a MutableList member in Kotlin

  29. 29

    iterator Overload Member Selection vs Indirection Operator

HotTag

Archive