1. If you want to explicitly grant access to a function that isn’t a member of the current structure, this is accomplished by declaring that function a friend inside the structure declaration. It’s important that the friend declaration occurs inside the structure declaration because you (and the compiler) must be able to read the structure declaration and see every rule about the size and behavior of that data type. And a very important rule in any relationship is, “Who can access my private implementation?”
2. You can declare a global function as a friend, and you can also declare a member function of another structure, or even an entire structure, as a friend.
3. Making a structure nested doesn’t automatically give it access to private members. To accomplish this, you must follow a particular form:
l first, declare (without defining) the nested structure
l then declare it as a friend
l and finally define the structure.
The structure definition must be separate from the friend declaration, otherwise it would be seen by the compiler as a non-member.
4. Difference between keyword class and struct: class defaults to private, whereas struct defaults to public.
5. Fragile base-class problem: any time you make a change to a class, whether it’s to the public interface or to the private member declarations, you’ll force a recompilation of anything that includes that header file. Solve this using handle class: in the declaration of the class, we can add a pointer of a struct, we put the definition of private members of this class in the definition of this struct.
评论