Why does C++ design friend functions and friend classes


First of all, we should mention java. For example, java defines a class 1.class, which will generate [bytecode] . Java has a reflection mechanism, which can find the class object and directly modify the private properties of the class. Gradually, the reflection mechanism has become a standard, and sun has become a standard, which is reflected in the API functions of JDK.



To be [precompiled] , compiled, linked, generated. . Corresponding to gcc

gcc  -E  hello .c  -o  hello .i (preprocessing)
 gcc  -S  hello .i  -o  hello .s (compile)
 gcc  -c  hello .s  -o  hello .o (assemble)
 gcc  hello .o  -o  hello (Link)

And if the assembly goes back, it will be very difficult.


So C++ opened the back door and used friend to destroy the encapsulation of the class. . . However, there are indeed some places where this mechanism needs to be used, which will be explained in the blog post after the specific application scenario.

Reprinted in: /zhangyaoqi/p/4591581.html

Leave a Comment

Your email address will not be published. Required fields are marked *