Home > Backend Development > C++ > What are some alternatives to friend functions?

What are some alternatives to friend functions?

王林
Release: 2024-04-16 17:18:01
Original
1112 people have browsed it

Alternatives to friend functions are: Encapsulating class methods: Define methods in the private part of the class and expose them as friend functions to maintain encapsulation and allow external access to private members. Bridge mode: Use the bridge class to contain a pointer to the target class, and add a friend function to it to delegate the target class method. Template metaprogramming: Use template metaprogramming to manipulate class members at compile time to allow access to private members.

What are some alternatives to friend functions?

An alternative to friend functions

A friend function is a special type of function that can access private members of other classes. Although friend functions are convenient, they also break encapsulation. Therefore, when designing a class, it is best to avoid using friend functions as much as possible.

The following are some alternatives to friend functions:

Encapsulating class methods:

Define a method in the private part of the class and encapsulate it Exposed as a friend function. This maintains encapsulation while still allowing external functions to access private members.

Use bridge mode:

Create a bridge class that contains a pointer to the destination class. Add the friend function to the bridge class, and then delegate the methods of the target class to the friend function.

Using template metaprogramming:

Use template metaprogramming to manipulate class members at compile time. This method is more complex, but allows access to private members at runtime.

Practical case:

Suppose we have a Person class whose private members are name and age . We need to define a printInfo function to print Person information.

// 使用封装类方法
class Person {
private:
    std::string name;
    int age;

    friend void printInfo(const Person& p) {
        std::cout << "Name: " << p.name << std::endl;
        std::cout << "Age: " << p.age << std::endl;
    }
};

// 使用桥接模式
class PersonBridge {
private:
    Person* person;

public:
    PersonBridge(Person* p) : person(p) {}

    void printInfo() {
        std::cout << "Name: " << person->name << std::endl;
        std::cout << "Age: " << person->age << std::endl;
    }
};

// 使用模板元编程
template <typename T>
void printInfo(const T& p) {
    std::cout << "Name: " << p.name << std::endl;
    std::cout << "Age: " << p.age << std::endl;
}
Copy after login

The above is the detailed content of What are some alternatives to friend functions?. 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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template