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

发布于 2024-12-11 11:08:42 字数 1127 浏览 0 评论 0原文

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

假设我有以下类声明

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";
      }
};

那么以下定义a的语句将导致编译错误。

int static_demo::a;

那么是否可以在类的私有部分中拥有静态数据成员呢?

按照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;
}

编译错误为:

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

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 a will result in compilation error.

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,

#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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

清晰传感 2024-12-18 11:08:42

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

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

int static_demo::a = 1;

定义成员时,访问说明符不会给您带来错误。访问说明符控制成员变量的访问,定义静态变量是允许的例外。

此处的 Ideone 上进行干净编译。

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

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

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.

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.

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.

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.

白鸥掠海 2024-12-18 11:08:42

在您的 .cpp 中:

int static_demo::a(0);

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

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

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

in your .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_demo is in the namespace MON, it would be defined in the .cpp:

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

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

治碍 2024-12-18 11:08:42

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

a 的定义是您在全局范围内使用 int static_demo::a; 所做的。
如果您希望 a 不以未定义的值开头,您只需要一个初始值设定项。

int static_demo::a = 1;

将解决问题。

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

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 a being private), you do an assignment.

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

int static_demo::a = 1;

will solve the problem.

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文