とあるコードを読んでいて、別ファイルのクラスを参照するときに、ヘッダファイルをインクルードする方法と、使用するクラスを自ファイルで宣言する方法があることを発見しました。しかし、どのように違うのかがわかりません。
コードを以下に示します。
質問したい箇所は、a.hの5、6行目です。
main.cpp
c++
1 2#include "a.h" 3#include "b.h" 4 5int main() 6{ 7 A a; 8 B b; 9 a.func(b); 10 return 0; 11}
a.h
c++
1#ifndef A_h 2#define A_h 3 4// class Bを参照するには以下の2つの方法がある 5class B; 6//#include "b.h" 7 8class A 9{ 10public: 11 void func(B& b); 12}; 13 14#endif
a.cpp
C++
1#include "a.h" 2 3void A::func(B& b) 4{ 5 return; 6}
b.h
c++
1#ifndef B_h 2#define B_h 3 4class B 5{ 6 int i; 7}; 8 9#endif
上記のa.hのなかで「class B;」を使うのと「#include "b.h"」を使うのとでは、どのような違いがあるのでしょうか?
回答3件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/07/22 01:14