当前位置:网站首页>Class and object finalization
Class and object finalization
2022-07-01 03:57:00 【__ cplusplus】
Main contents of this paper :
- Initialization list
- Static members , Member functions
- Friends
1. Initialization list
Today, let's wrap up classes and objects , Compared with the previous constructors and destructors , The content of this article is relatively easy to understand . Let's talk about static members in this blog , Member functions also have the initialization list mentioned a little earlier .
The so-called initialization list is where the member variables are defined ! You may have questions , When constructing an object , The whole object is out , Why do you have to find a place to define member variables . It is normal to have this question . Take a look at the following code :
class Demo
{
private:
int _a;
const int _b;//const Constants must be initialized at definition time
int& _rc;// References must also be initialized at definition time
};
It is precisely because the member variables of some classes must be initialized at the time of definition , So we must find a place to define . The place where the member is defined is the initialization list , So this Demo The constructor of a class can be written like this :
class Demo
{
public:
Demo(int a=4)
: _a(4)
, _b(3)
, _rc(a)
{
}
private:
int _a;
const int _b;
int& _rc;
};
in other words , Every time you call the constructor , All member variables will go through the initialization list once . More Than This , The initialization list can also be used in the following cases :
// The initialization list solves the problem that the default constructor cannot be generated
class A
{
public:
A(int x)
{
_x = x;
}
private:
int _x;
};
class Demo
{
public:
Demo(int a=4)
: _a(4)
, _b(3)
, _rc(a)
,_aa(5)
{
}
private:
int _a;
const int _b;
int& _rc;
A _aa;
};
Under normal circumstances , We can't provide Demo Default constructor for class , Because class A There is no default constructor , But we can solve this problem by using the initialization list .
in addition , The initialization order of the initialization list is only related to the declaration order of class members , It has nothing to do with the initialization sequence
class B
{
public:
B()
:_b1(4)
,_b2(_b1)
{
}
private:
int _b2;
int _b1;
};
int main()
{
B b;
return 0;
}
Open the monitor window , The following information was observed :
If you look at the order in the initialization list , Last _b1,_b2 The values of should be initialized to 4, But it's obvious that _b2 It is a random value . Because the initialization of the initialization list is in the order of member declaration ! So here we go first _b1 initialization _b2, therefore _b2 It's a random value ! Pay special attention to this when using the initialization list .
2. Static members , Static member functions
The members of our class depend on the instantiated object . So in C++ It also provides a special mechanism ----> Static members , Static member functions , Then these two things do not need to rely on objects ----> Can be interpreted as , Static members and static member functions are common to all class objects .
Let's take a look at how specific static members are defined :
class C
{
private:
int _a;
static char _c;
};
The initialization of static members is special , Due to the particularity of static members , So static members are not defined in the initialization list . We must explicitly initialize static members ourselves .
// Here's a demonstration , Put the static members out
class C
{
private:
int _a;
public:
static char _c;
};
// Static members must be initialized out of class , Because static members will not go through the initialization list
C::_c=1;
Finished talking about static members , Let's take a look at static member functions , The syntax declaration also adds a before the member function static keyword .
class Demo
{
public:
// This is a static member function
static void show()
{
cout<<"hello world!"<<endl;
}
};
There are two ways to access static member functions :
1. Use instanced objects to access
2. Use the class name :: Function name
Think about two questions :
1. Can ordinary member functions call static member functions ?
2. Can static member functions call ordinary member functions ?
The answer is : Ordinary member functions can call static member functions , Static member functions cannot be ordinary member functions . because , The static member function does not this The pointer !
3. Friends
Take the date class as an example , Sometimes we want to construct date objects by keyboard input , Then we need to overload the stream extraction operator on the date class , Suppose we overload the stream extraction operator into a member function
class Date
{
public:
Date(int year = 1900, int month = 1, int day = 1)
{
_year = year;
_month = month;
_day = day;
}
Date(const Date& d)
{
if (this != &d)
{
_year = d._year;
_month = d._month;
_day = d._day;
}
}
Date& operator=(const Date& d)
{
if (this != &d)
{
_year = d._year;
_month = d._month;
_day = d._day;
}
return *this;
}
// Standard library flow objects
istream& operator>>(istream& in)
{
in>>_year>>_month>>_day;
return in;
}
private:
int _year;
int _month;
int _day;
};
// Specific call
int main()
{
Date d1;
//cin>>d1 It's wrong.
d1>>cin;// This is the correct call !
return 0;
}
The reason for this is : A function of two operands , The compiler will automatically take the left operand as the first parameter . The first argument to a member function is always this The pointer , We can't change ! So we can write more natural and easy to understand , It is better to overload the global flow extraction operator . But there's another problem . Private member variables cannot be accessed directly from outside . therefore C++ Provides a special mechanism to solve ----> Friends
Let's first look at how to use friends to access private members :
class Date
{
public:
// Friend statement ---->friend keyword
friend istream& operator>>(Date& d);
Date(int year = 1900, int month = 1, int day = 1)
{
_year = year;
_month = month;
_day = day;
}
Date(const Date& d)
{
if (this != &d)
{
_year = d._year;
_month = d._month;
_day = d._day;
}
}
Date& operator=(const Date& d)
{
if (this != &d)
{
_year = d._year;
_month = d._month;
_day = d._day;
}
return *this;
}
private:
int _year;
int _month;
int _day;
};
// Overload global flow extraction
istream& operator(Date& d)
{
in>>d._year>>d._month>>d._day;
return in;
}
After the above operation , We can use cin To extract the date information !
Friends have the following points to pay attention to :
- Friendship is one-way ,A yes B Friends , however B My friend is not A!
- Friends are not transitive !
- Friends will destroy the encapsulation , Try to avoid using friends
summary
- The initialization list is where the members are defined
- Static members , Functions belong to classes , The static member function does not this The pointer
- Simple use of friends
That is the main content of this article , If there is insufficient support, please point out , I hope you can make progress together !
边栏推荐
- TS type gymnastics: illustrating a complex advanced type
- 241. Design priorities for operational expressions
- 206.反转链表
- 25.K个一组翻转链表
- Use of comment keyword in database
- 访问阿里云存储的图片URL实现在网页直接预览略缩图而不直接下载
- DO280管理应用部署--RC
- AfxMessageBox和MessageBox的用法
- Database DDL (data definition language) knowledge points
- 静态库使用MFC和共享库使用MFC的区别
猜你喜欢

The problem of integrating Alibaba cloud SMS: non static methods cannot be referenced from the static context

MFC window scroll bar usage

Download and installation configuration of cygwin

Idea plug-in backup table

DO280管理应用部署--RC

431. 将 N 叉树编码为二叉树 DFS
![[TA frost wolf \u may- hundred talents plan] 1.2.2 matrix calculation](/img/49/173b1f1f379faa28c503165a300ce0.png)
[TA frost wolf \u may- hundred talents plan] 1.2.2 matrix calculation

【TA-霜狼_may-《百人计划》】1.3纹理的秘密

【TA-霜狼_may-《百人计划》】2.3 常用函数介绍

Complete knapsack problem
随机推荐
【TA-霜狼_may-《百人计划》】1.1 渲染流水线
[ta- frost wolf \u may- hundred people plan] 2.2 model and material space
DO280管理应用部署--RC
431. 将 N 叉树编码为二叉树 DFS
Idea plug-in backup table
Download and installation configuration of cygwin
熊市下的Coinbase:亏损、裁员、股价暴跌
165. 比较版本号
【快捷键】
互联网行业最佳产品开发流程 推荐!
Visit the image URL stored by Alibaba cloud to preview the thumbnail directly on the web page instead of downloading it directly
What happens when a function is called before it is declared in C?
LetCode 1829. Maximum XOR value per query
Review column - message queue
Implement pow (x, n) function
30. 串联所有单词的子串
318. Maximum word length product
Appium automation test foundation -- supplement: c/s architecture and b/s architecture description
Volley parsing data shows networking failure
166. fractions to decimals