【C++进阶】类别互相引用(Class include each other)
来源:互联网 发布:彩票销售软件 编辑:程序博客网 时间:2024/06/14 23:14
[C/C++]類別互相引用(Class include each other)
前言
最近開始久違的寫C++程式,然後因為物件上的關係,所以某個A類別會擁有B類別,同時B類別會需要知道A類別
為了能夠壤兩個類別都能夠看到,所以依照直覺的想法,會使兩方的標頭檔(Header file)都會Include彼此,但是此時編譯器卻會發生問題,究竟是為什麼呢? 這樣的想法不是很合理嗎?
但其實編譯器在解讀時是會出現問題的,究竟哪裡有問題?
經過朋友的解說後,解決了問題也知道了原因,這邊來介紹一下給需要知道的人。
類別互相引用有時候在開發物件導向的程式時,免不了因為一些OO關係,需要讓某個A類別會擁有B類別,同時B類別會需要知道A類別:
標頭檔互相引用的錯誤的原因這邊我們拿A與B類別來解釋,前面提到「A類別會擁有B類別,同時B類別會需要知道A類別」,因為擁有B,所以需要include B,而B因為知道A,貌似直覺的做法B也會include A,此時在編譯的程序就會變成:
A class -> A Include B-> 進入B class -> B include A -> A class
所以會再次去A class的標頭檔(如下程式碼)
A.h
#ifndef A_H#define A_H#include "B.h"class A{public:A(void);~A(void);private:B b;};#endif
B.h
#ifndef B_H#define B_H#include "A.h"class B{public:B(void);~B(void);private:A *a; };#endif
依照常理來說,一般我們在寫標頭檔時,會使用 #ifndef 、#define、#endif的前置處理指令,確保只會被編譯過一次,之後被include多次時,不會再被編譯:
#ifndef A_H#define A_Hclass A{ //....}#endif
但是問題卻來,編譯時編譯A檔案,此發實現include B,便跳至B檔案,卻又發現B檔案有include A,再次跳回A檔案時,卻會因為A檔案先前編譯時,因為有加入#ifndef A_H與#define A_H的關係,定義過A_H,便不會再往下編譯A class,因此而又跳回B檔案,造成B檔案不認識A Class而在編譯其出錯。
但是,其實我們的B Class只是知道A(pointer)而已,根本不需要include A.h
解決方式-前置宣告(Forward Declartion)我們可以透過前至宣告的方式來告訴編譯器「先知道這個class的存在,至於他的定義後面會說明」。
只要不涉及生成或操作的話,前至宣告可以用在指標或參考類別。
然後再.cpp檔實際操作時,在include 類別標頭檔:
A.h
#ifndef A_H#define A_H#include "B.h"class A{public:A(void);~A(void);private:B b;};#endif
A.cpp
#include "A.h"A::A(void){}A::~A(void){}
B類別稍微修改一下:
B.h
#ifndef B_H#define B_Hclass A;class B{public:B(void);~B(void);private:A *a; };#endif
在cpp檔中,我們在來incldue A.h檔
B.cpp
#include "B.h"#include "A.h"B::B(void){}B::~B(void){}
以上的原因與解決方式,希望可以幫助到遇到此問題的人可以解惑!
- 【C++进阶】类别互相引用(Class include each other)
- [C/C++]類別互相引用(Class include each other)
- [C/C++]類別互相引用(Class include each other)
- 類別互相引用(Class include each other)
- [C/C++](Class include each other)
- C++头文件相互引用,最好一个#include,另一个class C;
- C语言存储类别(Storage Class)
- help each other
- funciton/procedure calling each other
- dlopen, cross call each other
- 谈一谈头文件引用(#include,#import,@import,@class)
- Class Extensions -匿名类别
- java 引用类别
- Let's learn from each other!
- use pipe to contact with each other
- MSVC 和 MinGW DLL call each other
- 第一单元 Getting to Know Each Other
- Qt and qml Call each other
- codeblocks快捷键
- (多项式运算4.7.6)POJ 2527 Polynomial Remains(多项式除法)
- 链接MySQL数据库出现CannotCreateTransactionException
- Android双向滑动菜单完全解析,教你如何一分钟实现双向滑动特效
- 集线器与交换机的区别
- 【C++进阶】类别互相引用(Class include each other)
- hdu 4089 Activation(概率dp)
- 僵尸进程《留给自己学着玩》
- Kinect for windows的脸部识别
- 修改MyEclipse eclipse 注释的作者名字
- Mac 可设置环境变量的位置、查看和添加PATH环境变量
- 矢量图形和矢量图形创建软件的修改
- 自定义的信号和槽注意点
- oracle小结