static member variable and static member function in a class
2008-04-28 03:17
435 查看
static member variable
In order to share data in different objects, and not destroy the principle of data hidden, keep safe. So static member is shared by all the objects of a class, and not the member of a given object. And it can save memory, as it only save in one location to shared by all the objects. The member can also be updated, we can ensure that the objects read the updated value, it's worthy.
To use a static member, we should pay attenen to
1. use static keyword when in definition
2. there is different between static member initialization and common data member initialization
<data type> <classname>::<static member name>=<value>
it means,
Initialization should be out of class, no static or private/public here;
The static member should be initialized before using, we should use the style <class name>::<static member name> to initialize.
If the memeber is public, we chould modify the value as the format above.
static member function
The same as static member variable, both are not belong to object, but class. So, we don't need object name to reference. We could not get nonstatic member variable directly, but we could get static member variable directly. Static member function will not use the default this pointer, it's why it could not use the nonstatic member variable. Maybe in sometimes, we sould use static function to read nonstatic member variable. Maybe in multithread, in fact, I'm not familiar with that. We said static member function don't have this pointer, why not pass this pointer by ourselves? We could write a function to return the objects address(pay attention to object), and we will get what we want. It only simulate the behavior of pass this pointer.
We could use static member in order to
1. save the number of objects (it's useful sometimes, as you want to know how many objects is generated)
2. as a flag to show status (we could know how something is working now, as file is open or not, printer is using or not)
3. save the first or the last address in a linked list
A simple example to use static member variable and static function
// file: object.h
#pragma once
#include <iostream>
class Object
...{
public:
Object()
: name("")
...{
number++;
}
Object(const std::string &name)
: name(name)
...{
number++;
}
// this function is only to simulate this pointer
const Object <
4000
/span>& pointer() const
...{
return (*this);
}
// static function to read static variable
static int getNumber()
...{
return number;
}
// only to show how to read nonstatic variable in a static function
static const std::string & getName(const Object & object)
...{
return object.pointer().name;
}
private:
std::string name;
private:
// set to private to prevent modified
static int number;
};
// file: object.cpp
#include "Object.h"
int Object::number = 0;
// file: main.cpp
#include <iostream>
#include "Object.h"
using namespace std;
int main (int argc, char * argv[])
...{
Object a("vincent");
cout<<a.getNumber()<<endl;
Object c,d("Jim"),e,f;
cout<<c.getNumber()<<endl;
cout<<d.getNumber()<<endl;
cout<<a.getNumber()<<endl;
// a.getName(a) and b.getName(a) are both OK, but not recommended
cout<<Object::getName(a).c_str()<<endl;
system("pause");
return 0;
}
In order to share data in different objects, and not destroy the principle of data hidden, keep safe. So static member is shared by all the objects of a class, and not the member of a given object. And it can save memory, as it only save in one location to shared by all the objects. The member can also be updated, we can ensure that the objects read the updated value, it's worthy.
To use a static member, we should pay attenen to
1. use static keyword when in definition
2. there is different between static member initialization and common data member initialization
<data type> <classname>::<static member name>=<value>
it means,
Initialization should be out of class, no static or private/public here;
The static member should be initialized before using, we should use the style <class name>::<static member name> to initialize.
If the memeber is public, we chould modify the value as the format above.
static member function
The same as static member variable, both are not belong to object, but class. So, we don't need object name to reference. We could not get nonstatic member variable directly, but we could get static member variable directly. Static member function will not use the default this pointer, it's why it could not use the nonstatic member variable. Maybe in sometimes, we sould use static function to read nonstatic member variable. Maybe in multithread, in fact, I'm not familiar with that. We said static member function don't have this pointer, why not pass this pointer by ourselves? We could write a function to return the objects address(pay attention to object), and we will get what we want. It only simulate the behavior of pass this pointer.
We could use static member in order to
1. save the number of objects (it's useful sometimes, as you want to know how many objects is generated)
2. as a flag to show status (we could know how something is working now, as file is open or not, printer is using or not)
3. save the first or the last address in a linked list
A simple example to use static member variable and static function
// file: object.h
#pragma once
#include <iostream>
class Object
...{
public:
Object()
: name("")
...{
number++;
}
Object(const std::string &name)
: name(name)
...{
number++;
}
// this function is only to simulate this pointer
const Object <
4000
/span>& pointer() const
...{
return (*this);
}
// static function to read static variable
static int getNumber()
...{
return number;
}
// only to show how to read nonstatic variable in a static function
static const std::string & getName(const Object & object)
...{
return object.pointer().name;
}
private:
std::string name;
private:
// set to private to prevent modified
static int number;
};
// file: object.cpp
#include "Object.h"
int Object::number = 0;
// file: main.cpp
#include <iostream>
#include "Object.h"
using namespace std;
int main (int argc, char * argv[])
...{
Object a("vincent");
cout<<a.getNumber()<<endl;
Object c,d("Jim"),e,f;
cout<<c.getNumber()<<endl;
cout<<d.getNumber()<<endl;
cout<<a.getNumber()<<endl;
// a.getName(a) and b.getName(a) are both OK, but not recommended
cout<<Object::getName(a).c_str()<<endl;
system("pause");
return 0;
}
相关文章推荐
- Java Static 修饰variable, function and class 用法
- 'static' can indeed be used in C++ to create a Static Member Function
- const and static value in class
- const type qualifier in class member function
- This function has none of Deterministic,no sql,or reads sql data in its declaration and binary logging is enabled(you *might* want to use the less safe log_bin_trust_function_creators variable
- static variable in web app, .net app domain, worker process and app pool
- static object member in class + access control
- How do I declare and use a pointer to a class member function?
- warning: in-class initialization of non-static data member is a C++11 extension [-Wc++11-extensions]
- [React] Define defaultProps and PropTypes as static methods in class component
- How to declare and initialize a static const array as a class member?
- functionclass[LeetCode]Construct Binary Tree from Preorder and Inorder Traversal
- How to place all WPF commands and related events centrally in a static class
- g++报错原因分析error: no......member function declared in class 'XXX‘
- Use Module and Function instead of Class in Python
- functionclass[LeetCode]Construct Binary Tree from Inorder and Postorder Traversal
- What is the difference between @staticmethod and @classmethod in Python?
- static function and variable
- Reading Effictive java: static member class (SMC) and nonstatic member class(NSC)
- Class method differences in Python: bound, unbound and static