Rules of operator overloading in C++

1,107 views
Skip to first unread message

abhishek gupta

unread,
Mar 14, 2019, 12:19:11 PM3/14/19
to std-dis...@isocpp.org
Hi,

I am having some trouble understanding operator overloading in c++.I have the following program-

class Abc{
    
int x=12;
    
double y=77;
    
public:
    
    
void operator+(){x+=10;}
    //-----d
void operator<<(){cout<<x<<endl;cout<<y<<endl;}  //ERROR - but it worked for '+'
   --- 'c'
};

int main() {
Abc ob;
ob.operator+();   //works fine ----- 'a'

ob+;              //ERROR ------    'b'
    
ob.operator<<();

return 0;

}

1)If statement 'a' works then why 'b' does not work?Aren't they both same?
2)Why do I get the compilation error for 'c'-

error: 'void Abc::operator<<()' must take exactly one argument
     void operator<<(){cout<<x<<endl;cout<<y<<endl;}

3)Is it necessary to overload << operator as friend function?

4)Similarly,is it a rule that overloading postfix operator requires a dummy argument & that to an int?

5)Is there a rule regarding which operator can be overloaded & out of them which can be member functions and which can be non-member functions?

I summary, I do not find any logical explanation regarding how an operator has to be overloaded.

abhishek gupta

unread,
Mar 16, 2019, 7:25:07 AM3/16/19
to std-dis...@isocpp.org
Hi,

Can someone please resolve my doubts here?

Andrew Tomazos

unread,
Mar 16, 2019, 7:32:12 AM3/16/19
to std-discussion
As I previously told you, these sort of questions would be more appropriate for StackOverflow:  www.stackoverflow.com

--

---
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Discussion" group.
To unsubscribe from this group and stop receiving emails from it, send an email to std-discussio...@isocpp.org.
To post to this group, send email to std-dis...@isocpp.org.
Visit this group at https://groups.google.com/a/isocpp.org/group/std-discussion/.

Bo Persson

unread,
Mar 16, 2019, 8:25:06 AM3/16/19
to std-dis...@isocpp.org
No, they are not the same. A unary operator+ is used as +ob.

> 2)Why do I get the compilation error for 'c'-
>
> error: 'void Abc::operator<<()' must take exactly one argument
>      void operator<<(){cout<<x<<endl;cout<<y<<endl;}

The compiler tells you that there is no version of operator<< that takes
no parameters.

A member operator takes one parameter (plus *this). A non-member takes
two parameters (and has no *this).

>
> 3)Is it necessary to overload << operator as friend function?

No, but is often a convenient way to access private class members.

>
> 4)Similarly,is it a rule that overloading postfix operator requires
> a dummy argument & that to an int?

Yes, but only for the postfix operators ++ and --. For other operators
you cannot choose how they work.

>
> 5)Is there a rule regarding which operator can be overloaded & out
> of them which can be member functions and which can be non-member
> functions?

Yes. Most can be either, but a few operators have to be members, the
assignment operator in particular. See

https://en.cppreference.com/w/cpp/language/operators

>
> I summary, I do not find any logical explanation regarding how an
> operator has to be overloaded.
>

There is a logic, but it is kind of complex (for various historical
reasons). You cannot learn this by testing or asking about examples, but
have to read up on this (preferrably in a book).


Bo Persson



Reply all
Reply to author
Forward
0 new messages