C++前置声明

一、c++中类的相互包含 

1.c++中include的使用方法。

#include<>引用的是编译器的类库路径里的头文件。先去系统目录中找头文件,如果没有在到当前目录下找。所以像标准的头文件 stdio.h、stdlib.h等用这个方法。

 #include" "引用的是程序目录里相对路径中的头文件。而""首先在当前目录下寻找,如果找不到,再到系统目录中寻找。 这个用于include自定义的头文件,让系统优先使用当前目录中定义的。

2.namespace的关键字调用:

 ①.直接指定标识符。例如std::ostream而不是ostream。完整语句如下: std::cout << std::hex << 3.4 << std::endl;

 ②.使用using关键字。 using std::cout; using std::endl; 以上程序可以写成 cout << std::hex << 3.4 << endl;

 ③.最方便的就是使用using namespace std;

二、c++中类的前置声明

c++的类中可以包含其他类,但是如果两个类相互包含的话就会出错:'XXX' does not name a type(因为#include的本质是将代码复制进去),这时使用前置声明就可以有效的解决这个问题。

例如一个Child.h

#ifndef CHILD_H

#define CHILD_H

#include <string>

using namespace std;

class Father;

class Child

{

private:

    Father *f;

public:

    Child(Father *father);

    string name;

    void answer();

    void callFather();

};

#endif // CHILD_H

 在这个Child类中前置声明了father类,并且初始化了一个Father类的指针*f。(注意:前置声明的类中没有声明的函数不能被在声明前被调用,前置声明只能作为指针或引用,不能定义类的对象,自然也不能调用对象中的方法。 )

再看看Child类方法的实现:

#include "child.h"

#include <iostream>

#include "father.h"

Child::Child(Father *father):f(father)

{

    name="xiaohua";

}

void Child::answer(){

    cout<<endl<<name<<" is here!";

}

void Child::callFather(){

    cout<<endl<<"I am calling my father!";

    f->answer();

    cout<<endl<<"Father is not here!";

}

 Child类的成员函数中通过类指针调用了Father类中的answer函数。 (void Father::answer(){ cout<<endl<<name<<" is here waiting for you!"; }) 通过前置声明类的方法,避免了类的相互包含,也能在一个类中调用另一个类。

#include "father.h"
#include <iostream>
#include "child.h"
Father::Father()
{
    name="laohua";
    child=new Child();
}

void Father::callChild(){
    cout<<endl<<"I am calling my child!";
    child->answer();
}

void Father::answer(){
    cout<<endl<<name<<" is here waiting for you!";
}

#ifndef FATHER_H
#define FATHER_H
#include "child.h"
#include <string>


using namespace std;

class Father
{
public:
    Father();
    string name;

    void callChild();
    void answer();
 private:
    Child *child;
};

#endif // FATHER_H

前置声明解决类的相互依赖:

1. // A.h  

2. class B;  

3. class A  

4. {  

5.     B* b;  

6. public:  

7.     A(void);  

8.     virtual ~A(void);  

9. };  

10.   

11. //A.cpp  

12. #include "B.h"  

13. #include "A.h"  

14. A::A(void)  

15. {  

16.     b = new B;  

17. }  

18.   

19.   

20. A::~A(void)  

21. {  

22. }  

23.   

24. // B.h  

25. class A;  

26. class B  

27. {  

28.     A a;  

29. public:  

30.     B(void);  

31.     ~B(void);  

32. };  

33.   

34. // B.cpp  

35. #include "A.h"  

36. #include "B.h"  

37. B::B(void)  

38. {  

39.     a = New A;  

40. }  

41.   

42.   

43. B::~B(void)  

44. {  

45. }  

 

1. // House.h  

2. class CBed;

3. class CHouse  

4. {  

5.     CBed* bed; 

6. public:  

7.     CHouse(void);  

8.     virtual ~CHouse(void);  

9.     void GoToBed();  

10. };  

11.   

12. // House.cpp  

13. #include "Bed.h"  

14. #include "House.h"

15.   

16. CHouse::CHouse(void)  

17. {  

18.     bed = new CBed();

19. }  

20.   

21. CHouse::~CHouse(void)  

22. {  

23. }  

24.   

25. void CHouse::GoToBed()  

26. {  

27.     bed->Sleep();  

28. }  

29.   

30. // Bed.h  

31. class CBed  

32. {  

33.   

34. public:  

35.     CBed(void);  

36.     ~CBed(void);  

37.     void Sleep();  

38. };  

39.   

40. // Bed.cpp  

41. #include "Bed.h"  

42.   

43. CBed::CBed(void)  

44. {  

45. }  

46.   

47.   

48. CBed::~CBed(void)  

49. {  

50. }  

51.   

52. void CBed::Sleep()  

53. {  

54.   

55. }  

 

1. // House.h  

2. class CBed; 

3. class CHouse  

4. {  

5.     CBed& bed;

6. public:  

7.     CHouse(void);  

8.     CHouse(CBed& bedTmp);  

9.     virtual ~CHouse(void);  

10.     void GoToBed();  

11. };  

12.   

13. // House.cpp  

14. #include "Bed.h"  

15. #include "House.h" // 等房子开始装修了,要买床了  

16.   

17. CHouse::CHouse(void)  

18.     : bed(*new CBed())  

19. {  

20.     CBed* bedTmp = new CBed();  

21.     bed = *bedTmp;  

22. }  

23.   

24. CHouse::CHouse(CBed& bedTmp)  

25.     : bed(bedTmp)  

26. {  

27. }  

28.   

29. CHouse::~CHouse(void)  

30. {  

31.     delete &bed;  

32. }  

33.   

34. void CHouse::GoToBed()  

35. {  

36.     bed.Sleep();  

37. }  

 

1. class CBed; 

2. class CHouse  

3. {  

4.     CBed* bed; 

5.    

6. public:  

7.     CHouse(void);  

8.     virtual ~CHouse(void);  

9.     void GoToBed()  

10.     {  

11.         bed->Sleep(); 

12.     }  

13. };  

 

1. class CBed; 

2. class CHouse  

3. {  

4.     CBed* bed; 

5. public:  

6.     CHouse(void);  

7.     virtual ~CHouse(void);  

8.     void GoToBed();  

9.     void RemoveBed()  

10.     {  

11.         delete bed; 

12.     }  

13. };  

14.   

15. // House.cpp  

16. #include "Bed.h"  

17. #include "House.h" // 等房子开始装修了,要买床了  

18.   

19. CHouse::CHouse(void)  

20. {  

21.     bed = new CBed(); // 把床放进房子  

22. }  

23.   

24. CHouse::~CHouse(void)  

25. {  

26.     int i = 1;  

27. }  

28.   

29. void CHouse::GoToBed()  

30. {  

31.     bed->Sleep();  

32. }  

33.   

34. // Bed.h  

35. class CBed  

36. {  

37.     int* num;  

38. public:  

39.     CBed(void);  

40.     ~CBed(void);  

41.     void Sleep();  

42. };  

43.   

44. // Bed.cpp  

45. #include "Bed.h"  

46.   

47. CBed::CBed(void)  

48. {  

49.     num = new int(1);  

50. }  

51.   

52. CBed::~CBed(void)  

53. {  

54.     delete num;  

55. }  

56.   

57. void CBed::Sleep()  

58. {  

59.   

60. }  

61.   

62. #include "House.h"  

63.   

64. int main()  

65. {  

66.     CHouse house;  

67.     house.RemoveBed();  

68. }  

 

前置声明在友元类方法中的应用

1. // House.h  

2. #include "Bed.h"  

3. class CHouse  

4. {  

5.     friend void CBed::Sleep(CHouse&);  

6. public:  

7.     CHouse(void);  

8.     virtual ~CHouse(void);  

9.     void GoToBed();  

10.     void RemoveBed()  

11.     {  

12.     }  

13. };  

14.   

15. // House.cpp  

16. #include "House.h"  

17.   

18. CHouse::CHouse(void)  

19. {  

20. }  

21.   

22. CHouse::~CHouse(void)  

23. {  

24.     int i = 1;  

25. }  

26.   

27. void CHouse::GoToBed()  

28. {  

29. }  

30.   

31. // Bed.h  

32. class CHouse;  

33. class CBed  

34. {  

35.     int* num;  

36. public:  

37.     CBed(void);  

38.     ~CBed(void);  

39.     void Sleep(CHouse&);  

40. };  

41.   

42. // Bed.cpp  

43. #include "House.h"  

44. CBed::CBed(void)  

45. {  

46.     num = new int(1);  

47. }  

48.   

49. CBed::~CBed(void)  

50. {  

51.     delete num;  

52. }  

53.   

54. void CBed::Sleep(CHouse& h)  

55. {  

56.   

57. }  

 

猜你喜欢

转载自blog.csdn.net/qq_34318986/article/details/61639871
今日推荐