在c ++中定义一堆静态方法 [英] defining bunch of static methods in c++

查看:116
本文介绍了在c ++中定义一堆静态方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是合适的:

  class xyz {
static int xyzOp1(){}
static int xyzOp2(){}
};

  namespace xyz {
static int xyzOp1(){}
static int xyzOp2(){}
};

当我们在与命名空间标签比较时使用类标签定义时, / p>

还有什么不同的内存管理,我们需要担心吗?

解决方案

没有看到这些函数的主体,我会说,命名空间更合适。使用命名空间,您可以使用使用语句,以便您不必在调用函数名称时完全限定函数名称。



使用类的唯一情况是静态方法与类的对象有任何关系,例如当他们需要访问实例的私有成员时。从您的描述中,似乎您不会创建任何xyz实例,因此您不应在此处使用类。



从内存管理的角度,这些方法没有区别。


Which is appropriate:

class xyz {
  static int xyzOp1() { }
  static int xyzOp2() { }
};

OR

namespace xyz {
  static int xyzOp1() {}
  static int xyzOp2() {}
};

Is there something specific which we can get when we define using class tag in comparision with namespace tag?

Also is there any different in memory management, which we need to worry?

解决方案

Without seeing the body of these functions, I would say that namespaces are more appropriate. With namespaces, you can have using statements, so that you don't have to fully-qualify the function names when calling them.

The only case in which to use classes is when the static methods have any relationship with objects of the class, e.g. when they need to access private members of instances. From your description, it seems that you won't be creating any instances of xyz, so you shouldn't be using classes here.

From a memory management point of view, there is no difference between these approaches.

这篇关于在c ++中定义一堆静态方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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