我在书上看到一个源代码里面有一句class CClientDlg;这样他就可以使用那个类的成员变量了,而不是用 
#include "ClientDlg.h"头文件,
用class CClientDlg的话的语句可以用#include "ClientDlg.h"替换吗,
我在论坛上有高人说当两个类互相引用时,常用前置声明;
而我在看源码时看到只有一边用class CClientDlg这种语句的,那当两个类在互相引用,具体应该哪边用,哪边不用呢,?

解决方案 »

  1.   

    例:
    //a.h
    //Class B
    //这里必须是有#include "b.h"
    #include "b.h"
    Class A {
      B b;
    }
    //b.h
    //这里可以不需要#include 'a.h'
    Class A;
    Class B{
      A* p;
    }
      

  2.   

    class CClientDlg;这种形式只是个声明。
    class ss
    {
    CClientDlg * pDlg; //right
    CClientDlg dlg; //wrong
    }--------------------------#include "ClientDlg.h"
    class ss
    {
    CClientDlg * pDlg; //right
    CClientDlg dlg; //right
    }
      

  3.   

    class CClientDlg和#include "ClientDlg.h"是不能替换的,一般的引用直接用include就行了,如果两个类互相引用,必须要前置声明
      

  4.   

    class CClientDlg和#include "ClientDlg.h"一个是声明一个是包含,不可替换。当需要相互引用对方时,需要先声明。