声明为私有时的静态成员变量
当类中的静态成员变量声明为私有时,如何定义它?
假设我有以下类声明
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
与在源(cpp)文件中定义公共静态变量的方式完全相同。
定义成员时,访问说明符不会给您带来错误。访问说明符控制成员变量的访问,定义静态变量是允许的例外。
在此处的 Ideone 上进行干净编译。
编辑:在发布代码后回答您的问题。
您的问题不是静态成员的定义。该错误是因为您试图访问
main
内的私有静态成员。你不能那样做。类的私有成员只能在类成员函数内部访问,同样的规则甚至适用于静态成员。为了能够修改/访问静态成员,您必须向类添加成员函数,然后修改/访问其中的静态成员。
In the very same way as you define a public static variable in your source(cpp) file.
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.
在您的 .cpp 中:
当这导致错误时,最常见的是您遇到了多重定义符号的链接器错误(例如定义位于标头中),或者您可能尝试在错误的范围内定义它。也就是说,如果
static_demo
位于命名空间 MON
中,它将在 .cpp 中定义:对于其他类型,它可能只是一个不正确的构造函数。
in your .cpp:
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 thenamespace MON
, it would be defined in the .cpp:for other types, it may simply have been an incorrect constructor.
问题不在于定义,而在于在 main() 中(不在 static_demo 的名称范围内,并且看不到
a
是私有的),您执行了一个赋值。a
的定义是您在全局范围内使用int static_demo::a;
所做的。如果您希望
a
不以未定义的值开头,您只需要一个初始值设定项。将解决问题。
从那时起,
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, withint static_demo::a;
.You simply need an initializer, if you want
a
not to start with an undefined value.will solve the problem.
From than on,
a
can be manipulated only by functions instatic_demo
(of friend of it).