一个类应该有静态成员还是非静态成员 [英] Should a class ever have static and non-static members

查看:128
本文介绍了一个类应该有静态成员还是非静态成员的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图找出什么时候让一个类同时具有静态和非静态功能是合适的.又名:

I'm trying to figure out when it would be appropriate for a class to have both static and non-static functions. AKA:

$obj = new ClassA;
$obj->doOOPStuff();

$something = ClassA::doStaticStuff();

注意:此示例在PHP中完成,但是问题与语言无关.

似乎,如果您有一个要实例化的类,则可以静态调用的任何函数很可能属于另一个类.

It seems that if you have a class that is meant to be instantiated, any functions that can be called statically, most likely belong in another class.

在任何可行的情况下,我会有一个使用静态和非静态成员的类吗?

Is there any viable cases where I would have a class that used static AND non-static members?

推荐答案

一个示例:何时必须以特定方式进行创建.

One example: when Creation has to happen in a specific way.

class Foo {
public:
  static Foo* Create(...params...);

private:
  Foo();
};

这篇关于一个类应该有静态成员还是非静态成员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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