正文

Thinking in C++:5 Implementation2006-10-21 21:17:00

【评论】 【打印】 【字体: 】 本文链接:http://blog.pfan.cn/wenzhuo316/19592.html

分享到:

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.

阅读(2392) | 评论(0)


版权声明:编程爱好者网站为此博客服务提供商,如本文牵涉到版权问题,编程爱好者网站不承担相关责任,如有版权问题请直接与本文作者联系解决。谢谢!

评论

暂无评论
您需要登录后才能评论,请 登录 或者 注册