Home > Backend Development > C++ > Friend Function vs. Member Function for Operator

Friend Function vs. Member Function for Operator

Mary-Kate Olsen
Release: 2024-12-14 16:07:16
Original
694 people have browsed it

Friend Function vs. Member Function for Operator

Operator<< in C : Friend Function vs. Member Function

In C , the operator<< can be defined either as a friend function or as a member function. This article explores the rationale behind choosing one over the other in specific scenarios.

Friend Function Approach for Operator<<

When defining operator<< as a friend function, the syntax typically involves the following:

friend bool operator<<(obj const&amp; lhs, obj const&amp; rhs);
Copy after login

This approach is recommended when the relationship comparison involves examining the private members of the class. Since friend functions have access to the private members of a class, they can perform the comparison directly.

Member Function Approach for Operator<<

In this approach, the operator<< is defined as a member function of the class:

ostream&amp; operator<<(obj const&amp; rhs);
Copy after login

This approach is appropriate when the comparison involves publicly accessible data or simple operations. However, it has a key limitation: if the comparison requires access to private members, the member function approach cannot be used.

Comparison of Approaches

Streaming Operations:
When defining operator<< for streaming operations, both friend functions and member functions can be used. However, friend functions must be used if the streaming operation needs to modify the stream object (e.g., adding line breaks).

Equality and Relationship Operators:
For operators such as ==, !=, <, >, and so on, it is preferred to define them as member functions. This approach allows for easy comparison of private members within the class. Additionally, it simplifies the code by avoiding the need for additional friend functions.

Example

Consider the following example of a Paragraph class with a to_str() method:

class Paragraph
{
    public:
        Paragraph(std::string const&amp; init) : m_para(init) {}

        std::string const&amp; to_str() const { return m_para; }
};
Copy after login

Friend Function Approach:

friend ostream &amp; operator<<(ostream &amp;os, const Paragraph&amp; p) {
    return os << p.to_str();
}
Copy after login

Member Function Approach:

ostream &amp; operator<<(ostream &amp;os) {
    return os << paragraph;
}
Copy after login

In this case, the friend function approach is preferred as it allows access to the private member m_para for streaming operations.

The above is the detailed content of Friend Function vs. Member Function for Operator. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template