当前位置:网站首页>Static member
Static member
2022-07-22 19:40:00 【wzh_ scuec】
Static members
Static data members
You can define static members of a class , It can realize data sharing among multiple objects of the same kind . The advantage of using static members of classes is :
- The name of the static member is in the scope of the class , Therefore, you can avoid conflicts with members of other classes or global object names
- Static members can implement encapsulation , Can be a private member , And global objects cannot
- Static members are associated with specific classes , The structure is clear
(1) Static data members
example :
class employee {
// Employee class definition
private:
int empNo;
int id;
char *name;
static int count; // Static data members
...
};
Static data members are special data members of classes , It takes the keyword static Start , The declaration form is :
class Class name {
// The class body
...
static Data member type List of data member names ; // Static data member declaration
...
};
for example :
class Data {
//Data Class definition
public:
static int count; // Static data members
int maxlevel; // Non static public data member
Data(int i=0) {
...,count++; } // Constructors
private:
int level; // Non static private data members
};
int Data::count=0; // Static data members are defined and initialized
count The purpose of the design is to count Data How many instantiated objects does the class have in total
Description of static data members :
(1) Usually , Non static data members exist in every object of class type , Static data members are independent of any object of the class , Separate storage space outside all objects . The space allocated for the object does not include the space occupied by static data members
(2) If only a class is declared and no object is defined , Then the non static data members of the class do not occupy storage space , Only when defining objects , To allocate space for the data members of the object . As long as the static member is defined in the class , Even if no object is defined , Space is also allocated for static data members , It can be referenced before the object is created
(3) When accessing static members, you also need to abide by public and private access rules
(4) Static data members must be defined outside the class once ( Only once ), Static members cannot be initialized by class constructors , Instead, it is initialized when it is defined outside the class . The way to define static data members is :
Data member type Class name :: Static data member name = Initialization ;
(5) Static data members can be used as default arguments , Non static data members cannot be used as default arguments , Because its value cannot be used independently of the object it belongs to . for example :
class Data {
//Data Class definition
...
Data& setbkcolor(int bkcolor);
static const int bkcolor = 5;
};
(6) With static data members , Data sharing is realized among objects , Therefore, you can not use global variables
class test {
private:
int x;
int y;
public:
static int num;
static int Getnum() {
x+=5; // error , Static member functions cannot call non static data members
num+=15;
return num;
}
};
int test::num=10;
int main()
{
test a;
cout<<test::num<<endl; // Output 10
text::num=20;
cout<<test::num<<endl; // Output 20
cout<<test::Getnum()<<endl; // Output 35
cout<<a.Getnum()<<endl; // Output 50
return 0;
}
Static member functions
Member functions can also be defined as static , Before declaring a function in a class, add static It becomes a static member function , The general form of the declaration is :
class Class name {
// The class body
...
static Return type Function name ( type 1 Parameter name 1, type 2 Parameter name 2,...);
...
};
for example :
static int getcount() {
return count; } // Static member functions
Just like static data members , Static member functions are part of a class , Not part of the object . If you want to call a public static member function outside the class , You can use the class scope operator (::) And calling static member functions through object names , for example :
cout<<Data::getcount()<<'\t'<<d.getcount();
The fundamental difference between static member functions and non static member functions is : Non static member functions have this The pointer , And static member functions don't have this The pointer . therefore , Static member functions cannot access non static members in this class . Static member functions are designed to access static data members
Static member functions cannot be declared as const
Examples of static members :
#include<iostream>
using namespace std;
class CTest {
public:
CTest() {
s_total++; id=s_total; cout<<" structure "<<id<<" "; }
~CTest() {
s_total--; cout<<" destructor "<<id<<" "; }
static int gettotal() {
return s_total; }
private:
static int s_total;
int id;
};
int CTest::s_total=0;
int main() {
CTest a,b,c;
CTest *p=new CTest;
cout<<" total ="<<CTest::gettotal()<<" ";
delete p;
cout<<" total ="<<CTest::gettotal()<<" ";
return 0;
}
Examples of static members
class Test {
public:
void init() {
}
static void output() {
}
};
int main()
{
Test::init();
Test::output();
return 0;
}
- Compilation error , Because non static member functions of a class cannot be called by class name init
Examples of static members
class Test {
public:
void init() {
}
static void output() {
}
};
int main()
{
Test t;
t.init();
t.output();
return 0;
}
- Compile and pass , Class objects can use static member functions and non static member functions
Examples of static members
class Test {
public:
void init() {
}
static void output() {
cout<<x<<endl; }
private:
int x;
};
int main()
{
Test t;
t.output();
return 0;
}
- Compilation error , Because non static members cannot be referenced in static member functions
- Static member functions belong to the entire class , Space is allocated before the class instantiates the object , The non static members of a class must have memory space after the class instantiates the object
Examples of static members
class Test {
public:
void init() {
output(); }
static void output() {
}
};
int main()
{
Test t;
t.output();
return 0;
}
- Compile and pass , Because non static member functions of classes can call static member functions , But not vice versa
Examples of static members
class Test {
public:
Test() {
m++; }
~Test() {
m--; }
static void output() {
cout<<m<<endl; }
private:
static int m;
};
int main()
{
Test t;
t.output();
return 0;
}
- link error , Because static data members of a class must be initialized before use
- If in main Add... Before the function int Point::m=0;, Recompile the link without error , Running the program will output 1
边栏推荐
- [take you to learn C, take you to fly] branch structure in Chapter 3 of C language programming (3rd Edition) of Zhejiang University Edition (exercise 3)
- Neo4j - Cypher 语法示例
- el-table获取当前行index的方法
- SI12T触摸按键芯片替代TMS12
- 【commons-beanutils专题】005- ConvertUtils 专题
- "Pilot Cup" is coming! Summon the strongest brain in scientific computing, 360000 prize pool waiting for you
- 自然语言处理NLP文本分类顶会论文阅读笔记(二)
- pytest接口自动化测试框架 | pytest断言
- Solution to unsuccessful (invalid) password modification of MySQL
- 【带你学c带你飞】第3章 分支结构(练习3.1 简单的猜数游戏)
猜你喜欢
pytest接口自动化测试框架 | 为什么要做pytest插件的二次开发
Rongyun handles political affairs: "small grid" can also achieve "big governance"
三层交换机/路由器OSPF配置详解【华为eNSP实验】
【数据挖掘工程师-笔试】2022年SHEIN 公司
自然语言处理NLP文本分类顶会论文阅读笔记(二)
万兴PDF专家v8.3.8.1253专业版
CI24R1低成本2.4G无线收发器芯片替代XN297精简版SI24R1
Research on the principle of Tencent persistence framework mmkv
mysql进阶(十七)Cannot Connect to Database Server问题分析
Rongyun ramble: Communication Center
随机推荐
Research on the principle of Tencent persistence framework mmkv
The force deduction method summarizes the number of 1252 odd value cells
7.17 minimum ticket price
Leetcode notes
mysql进阶(十七)Cannot Connect to Database Server问题分析
Force deduction solution summary 1175 prime number arrangement
Force deduction solution summary 648 word replacement
[take you to learn C, take you to fly] branch structure in Chapter 3 of C language programming (3rd Edition) of Zhejiang University Edition (exercise 3)
Fluent adjusts the drawing shape by dragging
DP4361国产六通道立体声D/A音频转换器芯片替代CS4361
MySQL optimization enforces the use of indexes
国产立体声音频数模D/A转换器DP4344替代兼容CS4344
Force deduction solution summary 814 binary tree pruning
C WinForm excel automatically inserts / edits annotations according to the current selection
Rongyun ramble: Communication Center
项目执行过程中有几个关键注意事项?
Force deduction solution summary 324 swing sequencing II
Glibc source code analysis
Pytest interface automated testing framework | why do you want to do the secondary development of pytest plug-ins
Pytest interface automation test framework | pytest assertion