Inline Function in Namespace Scope and Static Function in Class Scope

xiver77

Is there any semantic difference between the two in the title?

For example, I can write,

class Hi{
public:
  static void Print(){
    printf("hi\n");
  }
};

but also,

namespace Hi{
  inline void Print(){
    printf("hi\n");
  }
}

I'm of course assuming both of these definitions are in the header. Is it just a matter of style?

Cheers and hth. - Alf

A main difference is that namespaces can be extended, while classes can be inherited. This difference was important for "enum wrappers" in C++03. Some people strongly favored wrapping enums in classes, and others strongly favored wrapping them in namespaces, while perhaps most didn't care and didn't wrap.

Namespaces support argument dependent lookup, while classes don't (except for calls of friend functions defined in class definitions, and then it's really namespace ADL kicking in).

Classes support access control (public, protected, private) while namespaces don't. With namespaces the technique corresponding to private access is a nested namespace called detail or impl or some such. But this is just convention, not something the compiler can check and enforce.

I guess that since the above is what occurred to me first, it's probably the most relevant.

Worth noting: libraries that uses classes as a kind of faux namespace mechanism do exist (in particular I ran into an XML parser library of that kind), but they are very rare, and I doubt that any new libraries do this except for possible the above mentioned enum wrapping, which however was made less important by the scoped enums of C++11.

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

scope in jquery ready function

분류에서Dev

Reach root of function scope

분류에서Dev

angularjs directive scope function not working

분류에서Dev

angularjs scope function of a repeated directive

분류에서Dev

What's the difference between using function and using inline expression to set scope variable

분류에서Dev

Update outside scope variable with apply function

분류에서Dev

JS local and global scope variable and function

분류에서Dev

Scope of variables when calling function from find

분류에서Dev

angular directive - scope undefined inside function

분류에서Dev

$scope not binding inside function in Angular JS

분류에서Dev

python function: scope of function parameter initialized with default value

분류에서Dev

Output inside function scope doesn't match that of it's parent

분류에서Dev

Angularjs how to access scope data outside the angular-poller function

분류에서Dev

Scope of functions and variables within a function and/or let statement for lisp

분류에서Dev

Variable scope - php - Using variables from one function in another

분류에서Dev

How to pass a function handler from controller into directive isolated scope in AngularJs?

분류에서Dev

$scope variable being undefined despite being set inside a $watch function

분류에서Dev

How to call scope function with data from ng-repeat?

분류에서Dev

Calling function with restricted arguments that are already restricted in current scope

분류에서Dev

ng-click wont trigger, function not working. Could it be scope?

분류에서Dev

Function inside setTimeout doesn't modify $scope variables / CSS properties

분류에서Dev

Monkey patching scope: Outside function doesn't affect ERB

분류에서Dev

Class variables not in the scope of class functions

분류에서Dev

why class can call the no static function?

분류에서Dev

function ($ scope) 옆에 [ '$ scope'가 있고 [ '$ scope'가없는 경우의 차이점은 무엇입니까?

분류에서Dev

function expression with namespace

분류에서Dev

PHP7.3: How can I inherit a protected static property with the thightest possible scope without redeclaring it in the child class?

분류에서Dev

sizeof C++ class from static member function

분류에서Dev

C++, Passing non-static function pointer in a class constructor

Related 관련 기사

  1. 1

    scope in jquery ready function

  2. 2

    Reach root of function scope

  3. 3

    angularjs directive scope function not working

  4. 4

    angularjs scope function of a repeated directive

  5. 5

    What's the difference between using function and using inline expression to set scope variable

  6. 6

    Update outside scope variable with apply function

  7. 7

    JS local and global scope variable and function

  8. 8

    Scope of variables when calling function from find

  9. 9

    angular directive - scope undefined inside function

  10. 10

    $scope not binding inside function in Angular JS

  11. 11

    python function: scope of function parameter initialized with default value

  12. 12

    Output inside function scope doesn't match that of it's parent

  13. 13

    Angularjs how to access scope data outside the angular-poller function

  14. 14

    Scope of functions and variables within a function and/or let statement for lisp

  15. 15

    Variable scope - php - Using variables from one function in another

  16. 16

    How to pass a function handler from controller into directive isolated scope in AngularJs?

  17. 17

    $scope variable being undefined despite being set inside a $watch function

  18. 18

    How to call scope function with data from ng-repeat?

  19. 19

    Calling function with restricted arguments that are already restricted in current scope

  20. 20

    ng-click wont trigger, function not working. Could it be scope?

  21. 21

    Function inside setTimeout doesn't modify $scope variables / CSS properties

  22. 22

    Monkey patching scope: Outside function doesn't affect ERB

  23. 23

    Class variables not in the scope of class functions

  24. 24

    why class can call the no static function?

  25. 25

    function ($ scope) 옆에 [ '$ scope'가 있고 [ '$ scope'가없는 경우의 차이점은 무엇입니까?

  26. 26

    function expression with namespace

  27. 27

    PHP7.3: How can I inherit a protected static property with the thightest possible scope without redeclaring it in the child class?

  28. 28

    sizeof C++ class from static member function

  29. 29

    C++, Passing non-static function pointer in a class constructor

뜨겁다태그

보관