当前位置:网站首页>记录一次循环引用的问题
记录一次循环引用的问题
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();
边栏推荐
- Redis cluster mode
- Oracle 数据泵导表
- The select option in laravel admin contains a large amount of data
- 1.13 learning summary
- Notes on enterprise wechat development [original]
- MySQL enable logbin in Qunhui docker
- Simple personal summary of tp6 multi application deployment -- Part I [original]
- 1.14 learning summary
- digital image processing
- [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
猜你喜欢
PHP small factory moves bricks for three years - interview series - my programming life
Install dbeaver and connect Clickhouse
1.17 learning summary
Computer network high frequency interview questions
Performance test comparison between PHP framework jsnpp and thinkphp6
Mysql8.0 configuring my SQL in INI file_ mode=NO_ AUTO_ CREATE_ User can start
Laravel framework Alipay payment fails to receive asynchronous callback request [original]
There is no response to redirection and jump in the laravel constructor [original]
Notes on enterprise wechat development [original]
ctf [RoarCTF 2019]easy_ calc
随机推荐
PHP installation SSH2 extension
PHP design function getmaxstr to find the longest symmetric string in a string - [original]
Development trend and prospect forecast report of China's financial industry 2022-2028 Edition
Sixtool- source code of multi-functional and all in one generation hanging assistant
Redis cache message queue
Svn error command revert error previous operation has not finished; run ‘ cleanup‘ if
Guide de la pompe de données Oracle
Implementation of seven classes of BlockingQueue interface
Notes on enterprise wechat development [original]
Database design (I)
Resolve PHP is not an internal or external command
Use shell script to analyze system CPU, memory and network throughput
Minecraft 1.16.5 生化8 模组 1.9版本 1.18版本同步
Report on the "fourteenth five year plan" and future development direction of global and Chinese indoor vertical farms from 2022 to 2028
Understand CGI and fastcgi
Introduction to markdown grammar
Dameng database backup and restore
Daily tests
Database related knowledge
Laravel framework Alipay payment fails to receive asynchronous callback request [original]