当前位置:网站首页>Record a circular reference problem
Record a circular reference problem
2022-06-26 04:42:00 【Hello,C++!】
1、 explain : object A To the object B reference , At the same time B It also refers to objects A, Causes a circular reference .
1.1、 Examples of errors are as follows :
A.h
#pragma once
#include "B.h"
class A
{
public:
B b;
};
B.h
#pragma once
class A;
class B
{
public:
A a;
};
1.2、 Compiler error :

1.3、 The reason for the error :
stay A.h:2, Processing statements #include “B.h”, Expand the header file
stay B.h:5 It is carried out in class B Declare a A A member variable of type , And then class A Not yet declared
So compile error :‘A’ does not name a type
1.4、 There are two ways to solve the problem of circular dependency :
1. Use forward declarations (forward declaration)
2. Avoid circular references at the design level
2、 The statement in the preceding paragraph
2.1、 The role of forward declarations :
1. Unwanted include The header file , The introduction of a large number of header files will cause compilation to slow down
2. It can solve the case that two classes call each other circularly
2.2、 matters needing attention :
Classes that are not defined because of forward declarations are incomplete , therefore class A Can only be used to define pointers 、 quote 、 Or pointers and references to function parameters , Cannot be used to define objects , Or access the members of the class .
This is because we need to determine class B The size of the space occupied , And type A There is no definition yet, and the size cannot be determined , but A Is the pointer type size known , therefore Class B Can be used in A Define member variables .
2.3、 For the characteristics of forward declarations , Revised as follows :
1、 Change the object to an object pointer
2、B In the file of , Use A Make a statement before
Examples are as follows :
A.h
#pragma once
#include "B.h"
class A
{
public:
B* b;
};
B.h
#pragma once
class A;
class B
{
public:
A* a;
};
3、 Avoid circular references at the design level
explain : use C++ The polymorphic characteristics of , Extract abstract classes . Use the parent class pointer 、 Subclass objects solve circular dependencies , Let the concrete depend on the abstract parent class .
The implementation is as follows :
IA.h
class IA
{
}
A.h
#include "IA.h"
#include "B.h"
class A : public IA
{
B* b;
}
B.h
#include "IA.h"
class B
{
public:
B(IA* ia) {
m_ia = ia; }
private:
IA* m_ia;
}
main.cpp
IA* ia = new A();
边栏推荐
- Video label forbids downloading. The test is valid. Hide button. The test is valid at three points
- "Eight hundred"
- 2.9 learning summary
- Minecraft 1.16.5 biochemical 8 module 1.9 version 1.18 version synchronization
- Tp6 is easy to tread [original]
- PHP has the problem of using strtotime to obtain time in months and months [original]
- Thinkphp6 parsing QR code
- Install Damon database
- Nightmare
- How to carry out word-of-mouth marketing for enterprises' products and services? Can word of mouth marketing be done on behalf of others?
猜你喜欢

1.18 learning summary
![PHP design function getmaxstr to find the longest symmetric string in a string - [original]](/img/45/d8dae9e605a2f411683db7a2d40d0b.jpg)
PHP design function getmaxstr to find the longest symmetric string in a string - [original]

Physical design of database design (2)

Rdkit chemical formula molecular formula search

Performance test comparison between PHP framework jsnpp and thinkphp6

1.24 learning summary

1.12 learning summary

TP5 distinct method paging problem

Database design (I)

Stm8 MCU ADC sampling function is triggered by timer
随机推荐
2022.2.17
2021-01-31
digital image processing
Numpy general function
0622-马棕榈跌9%
NVM installation and use and NPM package installation failure record
条件查询
Multipass中文文档-移除实例
Rsync common error messages (common errors on the window)
Navicat connects the pit of shardingsphere sub table and sub library plug-ins
SSH password free login, my server password free login to the other server, the other server password free login to your server
Composer version rollback version switching
Oracle 数据泵导表
Nightmare
PHP installation SSH2 extension
Multipass中文文档-使用实例命令别名
Fastadmin always prompts sqlstate[23000]: integrity constraint violation: 1052 column 'ID' in order clause is am
Sort query
Introduction to markdown grammar
A method of quickly transplanting library function code to register code by single chip microcomputer