为什么/何时我想使用类数据成员而不定义类的对象?

时间:2015-02-11 18:40:25

标签: c++ class data-members

通过将公共部分中的数据成员定义为static变量,可以在C ++中使用类的数据成员而不定义该类的对象,如下面的代码示例所示。问题是,为什么/何时我想这样做?我该怎么办?

class ttime{
public:
    ttime(int h=0, int m=0, int s=0):hour(h), minute(m), second(s){}   //constructor with default intialization
    int& warning(){return hour;}
    void display()const{cout<<hour<<"\t";}
    static int hello; 
    ~ttime(){}

private:
    int hour;
    int minute;
    int second;
};


main()
{
    ttime:: hello=11310; //Is this the way to use hello without creating an object of the class?
    cout << ttime:: hello;

    ttime hi(9); 
    hi.display();
    hi.warning()++;//the user is able to modify your class's private data, which is really bad! You should not be doing this!
    hi.display();
}

5 个答案:

答案 0 :(得分:2)

将类成员变量声明为static实际上使它成为该类的所有实例共享的单例对象。这对于计数器,信号量和锁定以及其他类成员需要共享的其他类型的数据非常有用。

声明它public使该类的所有用户都可以访问它。但是,允许类变量可以通过类外的函数进行修改通常是一个坏主意。

另一方面,

声明它const是为类提供公开可读常量的常用方法。

示例

您的图书馆课程:

class Foo
{
public:
    // Version number of this code
    static const int    VERSION = 1;

private:
    // Counts the number of active Foo objects
    static int          counter = 0;

public:
    // Constructor
    Foo()
    {
        counter++;     // Bump the instance counter
        ...
    }

    // Destructor
    ~Foo()
    {
        counter--;     // Adjust the counter
        ...
    }
};

你图书馆的一些客户:

class Bar
{
public:
    // Constructor
    Bar()
    {
        // Check the Foo library version
        if (Foo::VERSION > 1)
            std::cerr << "Wrong version of class Foo, need version 1";
        ...
    }
};

在此示例中,VERSION是类的静态常量,在这种情况下,通知外部世界类中包含的代码版本。它可以通过语法Foo::VERSION访问。

另一方面,静态counter变量对于类是私有的,因此只有Foo的成员函数才能访问它。在这种情况下,它被用作活动Foo对象数量的计数器。

答案 1 :(得分:1)

我目前不熟悉静态的c ++语法。但在c ++ - cli(.net,Visual C ++)中,::是正确的 出于静力学的目的:
在许多情况下使用它们是有意义的。通常,当您想要存储属于类本身的信息(对于类的所有对象而言)时,而不是存储到单个对象/实例。

答案 2 :(得分:1)

即使最初不是为此目的而发明的,static constexpr structs数据成员也是模板元编程的支柱。只需查看limits标准库标题即可。

例如,我们可以在内置sizeof运算符周围定义包装器。虽然它本身没什么用处,但它有希望给出正确的想法。

#include <iostream>

template<typename T>
struct Calipers
{
  static constexpr auto size = sizeof(T);
};

int
main()
{
  std::cout << "short:   " << Calipers<short>::size   << "\n";
  std::cout << "int:     " << Calipers<int>::size     << "\n";
  std::cout << "long:    " << Calipers<long>::size    << "\n";
  std::cout << "float:   " << Calipers<float>::size   << "\n";
  std::cout << "double:  " << Calipers<double>::size  << "\n";
}

可能的输出:

short:   2
int:     4
long:    8
float:   4
double:  8

答案 3 :(得分:1)

如前所述,静态成员变量的作用是“全局”。变量,但在类名称空间内。 因此,它对于对象之间的计数器或共享资源很有用。

对于公共静态&#39;修饰符,很容易看到它在库中的使用,以提供对常量和通用功能的访问(静态方法)。

例如,输入库可能包含:

class KeyEvent
{
    public:
      static const int KEY_DOWN = 111;
      static const int KEY_UP = 112;
      ...
}

//And then in your code
#include <KeyEvent>

void poolEvent(Key *key)
{
    if(key->type() == KeyEvent::KEY_DOWN)
      ...
}

答案 4 :(得分:0)

它类似于全局变量,只是它没有在全局命名空间中定义。

你在引入名称空间之前编写的C ++代码中找到它,或者在模板元编程中它更有用。

一般情况下,我不建议在您的示例中使用它,并且希望尽可能避免全局状态。否则,您最终会得到难以测试的代码。