Make a class, test it,
Test the private data members, but they do not have a Set method,
Want to write some values directly to these private data members for testing,
Is there a way to force a value into it?
Friends can provide a mechanism to access private members (private data members and private methods), but you can also try to access by pointer offset. This way requires knowing the memory layout of the class, otherwise it is prone to errors.
的 Private members in a class are generally not allowed to be accessed by any function outside the class, but a friend can break this ban and it can access private members of the class (including data members and member functions). Friends can be functions defined outside the class, or the entire class defined outside the class. The former is called a friend function and the latter is called a friend class. Friends break the encapsulation of the class, it is another important object-oriented product of C ++.