zoukankan      html  css  js  c++  java
  • Namespace + functions versus static methods on a class 命名空间函数和类的静态方法对比

    source:http://stackoverflow.com/questions/1434937/namespace-functions-versus-static-methods-on-a-class

    By default, use namespaced functions.

    Classes are to build objects, not to replace namespaces.

    In Object Oriented code

    Scott Meyers wrote a whole Item for his Effective C++ book on this topic, "Prefer non-member non-friend functions to member functions". I found an online reference to this principle in an article from Herb Sutter:http://www.gotw.ca/gotw/084.htm

    The important thing to know is that: In C++ functions in the same namespace than a class belong to that class' interface. (because ADL will search those functions when resolving function calls)

    namespaced functions, unless declared "friend" have no access to the class' internals, whereas static methods have.

    This means, for example, that when maintaining your class, if you need to change your class' internals, you will need to search for side effects in all its methods, including the static ones.

    Extension I

    Adding code to a class' interface.

    In C#, you can add methods to a class even if you have no access to it. But in C++, this is impossible.

    But, still in C++, you can still add a namespaced function, even to a class someone wrote for you.

    See from the other side, this is important when designing your code, because by putting your functions in a namespace, you will authorize your users to increase/complete the class' interface.

    Extension II

    A side-effect of the previous point, it is impossible to declare static methods in multiple headers. Every methods must be declared in the same class.

    For namespaces, functions from the same namespace can be declared in multiple headers (the almost-standard swap function is the best example of that).

    Extension III

    The basic cooless of a namespace is that in some code, you can avoid mentioning it, if you use the keyword "using":

    #include<string>#include<vector>// Etc.{usingnamespace std ;// Now, everything from std is accessible without qualification
       string s ;// Okvector v ;// Ok}
    
    string ss ;// COMPILATION ERRORvector vv ;// COMPILATION ERROR

    And you can even limit the "pollution" to one class:

    #include<string>#include<vector>{using std::string ;
       string s ;// Okvector v ;// COMPILATION ERROR}
    
    string ss ;// COMPILATION ERRORvector vv ;// COMPILATION ERROR

    This "pattern" is mandatory for proper use of the almost-standard swap idiom.

    And this is impossible to do with static methods in classes.

    So, C++ namespaces have their own semantics.

    But it goes further, as you can combine namespaces in a way similar to inheritance.

    For example, if you have a namespace A with a function AAA, a namespace B with a function BBB, you can declare a namespace C, and bring AAA and BBB in this namespace with the keyword using.

    Conclusion

    Namespaces are for namespaces. Classes are for classes.

    C++ was designed so each concept is different, and is used differently, in different cases, as solution to different problems.

    Don't use classes when you need namespaces.

    And in your case, you need namespaces.

  • 相关阅读:
    使用python,批量生产条形码
    excel——VlookUp函数的使用
    MQTT消息队列压力测试
    shell脚本中,关于if,以及条件判断
    linux下的echo
    python对文件操作 r w a 文件复制/修改
    使用Appium进行iOS的真机自动化测试
    pycharm 解决PEP8问题,配置autopep8到菜单栏
    Python中list的合并
    Centos最小化安装后,不能使用yum命令的解决办法
  • 原文地址:https://www.cnblogs.com/lebronjames/p/3093073.html
Copyright © 2011-2022 走看看