static member variables
来源:互联网 发布:php自动加载机制 编辑:程序博客网 时间:2024/04/30 08:31
In the previous lesson on static member variables, you learned that classes can have member variables that are shared across all objects of that class type. However, what if our static member variables are private? Consider the following example:
class
Something
{
private
:
static
int
s_nValue;
};
int
Something::s_nValue = 1;
// initializer
int
main()
{
// how do we access Something::s_nValue?
}
In this case, we can’t access Something::s_nValue directly from main(), because it is private. Normally we access private members through public member functions. While we could create a normal public member function to access s_nValue, we’d then need to instantiate an object of the class type to use the function! We can do better. In this case, the answer to the problem is that we can also make member functions static.
Like static member variables, static member functions are not attached to any particular object. Here is the above example with a static member function accessor:
class
Something
{
private
:
static
int
s_nValue;
public
:
static
int
GetValue() {
return
s_nValue; }
};
int
Something::s_nValue = 1;
// initializer
int
main()
{
std::cout << Something::GetValue() << std::endl;
}
Because static member functions are not attached to a particular object, they can be called directly by using the class name and the scope operator. Like static member variables, they can also be called through objects of the class type, though this is not recommended.
Static member functions have two interesting quirks worth noting. First, because static member functions are not attached to an object, they have no this pointer! This makes sense when you think about it — the this pointer always points to the object that the member function is working on. Static member functions do not work on an object, so the this pointer is not needed.
Second, static member functions can only access static member variables. They can not access non-static member variables. This is because non-static member variables must belong to a class object, and static member functions have no class object to work with!
Here’s another example using static member variables and functions:
class
IDGenerator
{
private
:
static
int
s_nNextID;
public
:
static
int
GetNextID() {
return
s_nNextID++; }
};
// We'll start generating IDs at 1
int
IDGenerator::s_nNextID = 1;
int
main()
{
for
(
int
i=0; i < 5; i++)
cout <<
"The next ID is: "
<< IDGenerator::GetNextID() << endl;
return
0;
}
This program prints:
Note that because all the data and functions in this class are static, we don’t need to instantiate an object of the class to make use of it’s functionality! This class utilizes a static member variable to hold the value of the next ID to be assigned, and provides a static member function to return that ID and increment it.
Be careful when writing classes with all static members like this. Although such “pure static classes” can be useful, they also come with some potential downsides. First, because all of the members belong to the class, and the class is accessible from anywhere in the program, it’s essentially the equivalent of declaring a global variable of the class type. In the section on global variables, you learned that global variables are dangerous because one piece of code can change the value of the global variable and end up breaking another piece of seemingly unrelated code. The same holds true for pure static classes. Second, because all static members are instantiated only once, there is no way to have multiple copies of a pure static class (without cloning the class and renaming it). For example, if you needed two independent IDGenerators, this would not be possible.
- static member variables
- Static member variables in C++
- python global variables,member variables,instance variables
- static variables and functions
- Java Static Variables
- Sicily 1002. Static Member
- C++类模板中静态成员变量的隐式实例化问题(Implicit initialization of static member variables for template classes)
- 12.2 Default values?Static variables.
- Behind C++ [2]- Static Variables
- Static variables in c programming
- static member variable and static member function in a class
- template class static member initialize
- Static Member of Derived Class
- Static member functions in C++
- class static member 重名问题
- static 和 no static Member function学习
- C++ freeing static variables referring heap resource
- python的static class method and member
- dataGridView 新增自动排序
- In all of the functions
- Java String Buffer
- python移植到arm
- VMware虚拟机优化10种技巧
- static member variables
- 给定0到4的随机数的方法,产生0到6的随机数的方法
- 大突破!人类有望饿死癌细胞
- 淘宝橱窗
- 黑马程序员 Java基础 多线程技术
- js easyUI treegrid 递归构造树
- Ajax工作原理
- 解读SQL Server2008的新语句MERGE
- WebServices 实现调用过程