当前位置:网站首页>记录一次循环引用的问题
记录一次循环引用的问题
2022-06-26 04:31:00 【Hello,C++!】
1、说明:对象A对对象B进行引用,同时对象B也引用到了对象A,导致循环引用。
1.1、错误示例如下:
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、编译报错:

1.3、错误原因:
在A.h:2,处理语句#include “B.h”,进行头文件展开
在B.h:5进行的是在class B中声明一个A类型的成员变量,而此时class A还没有被声明
因此编译报错:‘A’ does not name a type
1.4、解决循环依赖的问题有两种方式:
1.使用前向声明(forward declaration)
2.设计层面避免循环引用
2、前项声明
2.1、前向声明的作用:
1.不需要include头文件,大量引入的头文件会导致编译变慢
2.可以解决两个类相互循环调用的情况
2.2、注意事项:
由于前向声明而没有定义的类是不完整的,所以class A只能用于定义指针、引用、或者用于函数形参的指针和引用,不能用来定义对象,或访问类的成员。
这是因为需要确定class B空间占用的大小,而类型A还没有定义不能确定大小,但A是指针类型大小已知,因此Class B中可以使用A定义成员变量。
2.3、针对前向声明的特点,修改如下:
1、将对象改成对象指针
2、B的文件中,使用A之前进行声明
示例如下:
A.h
#pragma once
#include "B.h"
class A
{
public:
B* b;
};
B.h
#pragma once
class A;
class B
{
public:
A* a;
};
3、设计层面避免循环引用
说明:采用C++的多态特性,提取抽象类。使用父类指针、子类对象的方式解决循环依赖,让具体依赖抽象的父类。
实现方式如下:
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();
边栏推荐
- Simple personal summary of tp6 multi application deployment -- Part I [original]
- 1064 (42000) error occurred when installing MySQL and modifying root password
- Review of number theory
- Motivational skills for achieving goals
- Redis cache message queue
- CTF serialization and deserialization
- Thinkphp6 using kindeditor
- SixTool-多功能多合一代挂助手源码
- PIP batch complete uninstall package
- Your requirements could not be resolved
猜你喜欢

SixTool-多功能多合一代挂助手源码

Svn error command revert error previous operation has not finished; run ‘ cleanup‘ if

Realize video call and interactive live broadcast in the applet
![[H5 development] 02 take you to develop H5 list page ~ including query, reset and submission functions](/img/39/64df931d5ec54d7d19ae444fa372ba.jpg)
[H5 development] 02 take you to develop H5 list page ~ including query, reset and submission functions

Install dbeaver and connect Clickhouse

NPM installation tutorial

Performance test comparison between PHP framework jsnpp and thinkphp6

1.17 learning summary

微软禁止俄用户下载安装Win10/11

How to use the configured slave data source for the scheduled task configuration class scheduleconfig
随机推荐
SQL related knowledge - constraints
防撤回测试记录
"Eight hundred"
微软禁止俄用户下载安装Win10/11
Construction of art NFT trading platform | NFT mall
Report on demand situation and development trend of China's OTC industry from 2022 to 2028
Solution to composer error could not find package
[H5 development] 01 take you to experience H5 development from a simple page ~ the whole page implementation process from static page to interface adjustment manual teaching
Mysql8.0 configuring my SQL in INI file_ mode=NO_ AUTO_ CREATE_ User can start
Guide de la pompe de données Oracle
修改Oracle连接数
The statistics in the MySQL field become strings, and then they are converted into numbers for sorting
What are the advantages and risks of paper gold investment
Group by and order by are used together
What should I do if I don't understand the precious metal indicators
1.11 learning summary
NPM installation tutorial
Laravel pay payment access process
Tips for using idea
35 year old programmer fired Luna millions of assets and returned to zero in three days. Netizen: it's the same as gambling