简体   繁体   English

私有静态类成员

[英]Private static class members

When we declare a member variable static, it is shared between all instances of the class. 当我们声明一个成员变量static时,它在该类的所有实例之间共享。 I've heard that you should think of the variable belonging to the class itself, not any instance. 我听说你应该想到属于类本身的变量,而不是任何实例。 This lets us initialize the variable without instantiating any object of the class, which makes sense. 这让我们初始化变量而不实例化类的任何对象,这是有道理的。

class Something
{
  public:
    static int s_nValue;
};

int Something::s_nValue = 1;

But why are we allowed to initialize a private static member? 但是为什么我们允许初始化私有静态成员?

class Something
{
   private:
      static int s_nValue;
};

int Something::s_nValue = 1;

Does private even mean anything when we are talking about static members? 当我们谈论静态成员时,私人甚至意味着什么?

Yes, it does mean something. 是的,它确实意味着什么。 Consider the following example, which throws a compiler error, because the member is private . 请考虑以下示例,该示例抛出编译器错误,因为该成员是private Being able to initialize a private variable is not the same as being able to change it from any context. 能够初始化私有变量与能够从任何上下文更改它不同。

class Something
{
  private:
    static int s_nValue;
};

int Something::s_nValue = 1;

int main(){
    Something::s_nValue = 2; // Compiler error here.
}

Private still means the same thing: you cannot use the name Something::s_nValue except in the definition of a member of Something (or a friend, or a nested class within Something ). 私有仍然意味着同样的事情:你不能使用名称Something::s_nValue除了Something的成员(或朋友,或Something的嵌套类)的定义。

int Something::s_nValue = 1;

is the definition of a member of Something - namely, that static member s_nValue . Something成员的定义 - 即静态成员s_nValue

int Something::another_static_val = s_nValue;  // also okay
int OtherClass::x = Something::s_nValue;       // Illegal access!

int Something::getValue() const {
    return s_nValue;                // okay, getValue is a member of same class
}

int regularFunction() {
    return Something::s_nValue;     // Illegal access!
}

Does private even mean anything when we are talking about static members? 当我们谈论静态成员时,私人甚至意味着什么?

I'll try to answer with a classic example. 我将试着回答一个经典的例子。 Consider the following piece of code: 考虑以下代码:

#include <iostream>

class foo {
  static int count;
  int id;
public:
  foo() : id(++count) {}
  int getid() const { return id; }
};

int foo::count = 0;

int main() {
  foo f1, f2, f3;

  std::cout << f1.getid() << std::endl;
  std::cout << f2.getid() << std::endl;
  std::cout << f3.getid() << std::endl;
}

LIVE DEMO 现场演示

In the example above we use a private static int to count the instances of foo created. 在上面的例子中,我们使用private static int来计算创建的foo的实例。 We made the count static member variable private because we don't want anyone else except object of type foo to mess with it. 我们将count static成员变量设为private因为我们不希望除foo类型的对象之外的任何其他人都搞乱它。

And this is only a naive example, think of the possibilities. 这只是一个天真的例子,想想可能性。

Public, private and protected are properties of a class and not of an object. Public,private和protected是类的属性,而不是对象的属性。 Their purpose is to let you specify which parts of this class are visible to other classes, and not to hide stuff from objects of the same class. 他们的目的是让你指定这个的部分是可见的其他类,而不是从同一个类的对象隐藏的东西。 So, you can write code like this : 所以,你可以写这样的代码:

class A
{
public:
bool operator<(const A& other)
{
return this->val < other.val;
}
private:
int val;
};

So, private makes sense even when applied to static members - it just says that other classes cannot see this member. 因此,即使应用于静态成员,private也有意义 - 它只是说其他类无法看到此成员。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM