Notes for C ++ Operator Overloading

Source: Internet
Author: User

Sometimes you need to reload the operator to write a class, but some operators (such as =, <,>) can be written in the class or out-of-class. So how will the C ++ compiler call it?

First, we know that the "overload" mechanism allows multiple functions to have the same function name, but the parameter list must be different. After name mangling, the C ++ compiler can find the "most consistent" function entity based on the passed real parameters for calling. This is also true for operator overloading.

First, we have a class:
[Cpp]
Class CMyCls
{
Public:
Bool operator = (const CMyCls & rhs); // 1.

Bool operator = (const CMyCls & rhs) const; // 2.
 
Bool operator = (CMyCls & rhs); // 3.
 
Bool operator = (CMyCls & rhs) const; // 4.
};
 
 
Void f1 (CMyCls & lhs, const CMyCls & rhs );
{
Lhs = rhs;
}
 
Void f2 (const CMyCls & lhs, const CMyCls & rhs );
{
Lhs = rhs;
}
 
Void f3 (CMyCls & lhs, CMyCls & rhs );
{
Lhs = rhs;
}
 
Void f4 (const CMyCls & lhs, CMyCls & rhs)
{
Lhs = rhs;
}

So how will the four f1-f4 functions be called? In fact, the f1 function will be adjusted to the one in the CMyCls class to comment on the underlying function, f2 will be adjusted to the two to comment on the underlying function, f3, f4 and so on.

So, if I have an operator overload in the class, which one will be called outside the class? For example:
[Cpp]
Class CMyCls
{
Public:
Bool operator = (const CMyCls & rhs) const
{
//...
}
};
 
 
Bool operator = (const CMyCls & lhs, const CMyCls & rhs)
{
//...
}
 
 
Void f (const CMyCls & lhs, const CMyCls & rhs)
{
Lhs = rhs;
}
But unfortunately, it cannot be compiled. For f function calls, the compiler does not know which one is better, because there are two "most suitable" overload functions at the same time, which leads to ambiguity. However, this can be edited. Think about why:
[Cpp]
Class CMyCls
{
Public:
Bool operator = (const CMyCls & rhs)
{
//...
}
};
 
 
Bool operator = (const CMyCls & lhs, const CMyCls & rhs)
{
//...
}
 
 
Void f1 (const CMyCls & lhs, const CMyCls & rhs)
{
Lhs = rhs;
} Www.2cto.com
 
 
Void f2 (CMyCls & lhs, const CMyCls & rhs)
{
Lhs = rhs;
}

The above f1 will be adjusted to the global function operator =, while f2 will be adjusted to the member function operator = of CMyCls. Therefore, it is best to write the statement that only two parameters in the class or out-of-class overload are the version of const.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.