非会员职能 [英] Non member functions

查看:74
本文介绍了非会员职能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

更多非成员函数示例,用于粘合"类.

More examples of non-member functions to ''glue'' classes.
And the alternative approaches are?

推荐答案

替代方法是:避免使用非成员函数.严格来说,您永远不需要一个(应用程序入口点"main":-除外).每个非成员函数都可以用类的静态函数正式替换.对于静态函数,OOP通常会减少它们并有助于将其保持在最低水平.同时,如果函数不使用"this"(访问实例),则不应将其作为实例函数-make是静态的.

同样,将类/实例粘合在一起根本不是问题.这有什么特别之处?它是一般编程中不可或缺的一部分.再一次,它永远不需要非会员.
您是指Singleton模式吗?是的,有时您需要单身人士, http://en.wikipedia.org/wiki/Singleton_pattern [
Alternative approach is: avoid non-member functions. Strictly speaking, you never need one (except application entry point, "main" :-). Each and every non-member function can be formally replaced with static function of a class. As to static functions, OOP usually reduces them and helps to keep them to minimum. At the same time, if a function does not use "this" (access to the instance), is should not be an instance function — make is static.

Also, gluing classes/instances together is not a problem at all. What so special about this? It''s an integral part of general programming. Again, it never requires non-members.
Is what you mean a Singleton pattern? Yes, sometimes you need singletons, http://en.wikipedia.org/wiki/Singleton_pattern[^].

Start forgetting non-members. Get to OOP!

—SA


这篇关于非会员职能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆