C++ 声明为私有时的静态成员变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7857989/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 17:36:33  来源:igfitidea点击:

static member variable when declared private

c++static-members

提问by nitin_cherian

When a static member variable is declared private in a class, how can it be defined?

当一个静态成员变量在类中被声明为私有时,如何定义它?

Suppose i have the following class declaration

假设我有以下类声明

class static_demo
{
   private:
      static int a;

   public:
      static int b;

      void set(int x, int y)
      {
         a = x;
         b = y;
      }

      void show()
      {
         cout << "a = " << a << "\n";
         cout << "b = " << b << "\n";
      }
};

Then the following statement to define awill result in compilation error.

那么下面要定义的语句a会导致编译错误。

int static_demo::a;

So is it possible to have a static data member in the private section of class?

那么是否有可能在类的私有部分中有一个静态数据成员?

Adding full code as per Greg,

根据 Greg 添加完整代码

#include <iostream>

using namespace std;

class static_demo
{
   private:
      static int a;

   public:
      static int b;

      void set(int x, int y)
      {
         a = x;
         b = y;
      }
};

int static_demo::a;
int static_demo::b;

int main()
{
   static_demo::b = 10;
   static_demo::a = 20;

   return 0;
}

The compilation error is:

编译错误是:

static_member_variable.cpp: In function `int main()':
static_member_variable.cpp:20: error: `int static_demo::a' is private
static_member_variable.cpp:26: error: within this context

回答by Alok Save

When a static member variable is declared private in a class, how can it be defined?

当一个静态成员变量在类中被声明为私有时,如何定义它?

In the very same way as you define a public static variable in your source(cpp) file.

与在源(cpp)文件中定义公共静态变量的方式完全相同。

int static_demo::a = 1;

int static_demo::a = 1;

Access specifiers will not give you error while defining the member. Access specifiers control the access of the member variables, defining a static variable is an excpetion which is allowed.

访问说明符在定义成员时不会出错。访问说明符控制成员变量的访问,定义静态变量是允许的例外。

Compiles cleanly on Ideone here.

此处在 Ideone 上干净地编译。

EDIT:To answer your Q after you posted code.
Your problem is not the definition of the static member. The error is because you are trying to access the private static member inside main. You cannot do that.

编辑:在您发布代码后回答您的问题。
您的问题不是静态成员的定义。错误是因为您试图访问main. 你不能这样做。

Private members of a class can only be accessed inside the class member functions, the same rule applies even to static members. To be able to modify/access your static members you will have to add a member function to your class and then modify/access the static member inside it.

类的私有成员只能在类成员函数内部访问,同样的规则也适用于静态成员。为了能够修改/访问您的静态成员,您必须向您的类添加一个成员函数,然后修改/访问其中的静态成员。

回答by justin

in your .cpp:

在您的 .cpp 中:

int static_demo::a(0);

when this causes an error, it is most common that you either encountered a linker error for a multiply defined symbol (e.g. the definition was in the header), or that you may have tried to define it in the wrong scope. that is, if static_demois in the namespace MON, it would be defined in the .cpp:

当这导致错误时,最常见的情况是您遇到了多重定义符号的链接器错误(例如,定义在标题中),或者您可能试图在错误的范围内定义它。也就是说,如果static_demo在 中namespace MON,它将在 .cpp 中定义:

#include "static_demo.hpp"
int MON::static_demo::a(0);

for other types, it may simply have been an incorrect constructor.

对于其他类型,它可能只是一个不正确的构造函数。

回答by Emilio Garavaglia

The problem is not the definition, but the fact that in main() (that's not in the name scope of static_demo, and cannot see abeing private), you do an assignment.

问题不在于定义,而在于在 main() 中(不在 static_demo 的名称范围内,并且看不到a私有),您进行了赋值

The definition of ais what you did at global scope, with int static_demo::a;. You simply need an initializer, if you want anot to start with an undefined value.

的定义a是您在全局范围内所做的事情,使用int static_demo::a;. 如果您不想a以未定义的值开始,您只需要一个初始化程序。

int static_demo::a = 1;

will solve the problem.

将解决问题。

From than on, acan be manipulated only by functions in static_demo(of friend of it).

从那时起,a只能由static_demo(它的朋友)中的函数操作。