Advantages of classes with only static methods in C++

前端 未结 8 1689
谎友^
谎友^ 2020-12-01 07:28

Even though there are no static classes in C++, coming from a Java background I use to create a helper class like Util containing only static methods. Is this c

8条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-01 08:22

    There is no real issue with declaring static methods within a class. Although namespaces are more suitable for this purpose for the reasons mentioned in the post you are referring to.

    Using C functions can generate name collisions, unless you decide on a naming convention, prefixing your functions with stuff, for example btFunctionA, btFunctionB etc. You will want to keep your symbols within namespaces to avoid that, you are using C++ and not C after all.

    Static functions within a namespace aren't any different from non-static. I believe the static keyword is simply ignored in this context.

提交回复
热议问题