今天照做个例子,从CSocket派生了个类?我声明成一个变量,怎么也通不过?后来换了下声明成一个指针变量就通过了?不知道为什么?想问下什么情况下变量一定要声明成指针?什么时候指针一般类型皆可??谢谢回复!!!!!!!!!!!!感激.........

解决方案 »

  1.   

    我能想到的有如下两种情况:
    (1)你在此类的声明中,定了了此类的对象。
    Class CMySocket
    {
         CMySocket m_socket;//这个是不允许的,因为定义对象要分配空间,这个时候还不知道空间大小。
          CMySocket* m_pSocket;//这个是可以的,因为指针空间大小为定值。
    }(2)在其它类中没有包含类的头文件,而是使用class CMySocket方式声明。
    class CMySocket;Class COther
    {
         CMySocket m_socket;//这个是不允许的.
        CMySocket* m_pSocket;//这个是可以的
    }
      

  2.   

    把你的代码帖出来看看,就Socket而言,声明成指针或者一般变量都是可以的,是不是你在CMySocket中添加了其他特殊的成员变量。
      

  3.   


    #include "MyServerDlg.h"
    #include "afxsock.h"
    class CMyServerDlg;
    /////////////////////////////////////////////////////////////////////////////
    // CClientSock command targetclass CClientSock : public CSocket
    {
    // Attributes
    public:// Operations
    public:
    CClientSock(CMyServerDlg* dlg);
    virtual ~CClientSock();// Overrides
    public:
    CMyServerDlg* m_pDlg; // ClassWizard generated virtual function overrides
    //{{AFX_VIRTUAL(CClientSock)
    public:
    virtual void OnReceive(int nErrorCode);
    //}}AFX_VIRTUAL // Generated message map functions
    //{{AFX_MSG(CClientSock)
    // NOTE - the ClassWizard will add and remove member functions here.
    //}}AFX_MSG// Implementation
    protected:
    };CClientSock::CClientSock(CMyServerDlg* dlg)
    :m_pDlg(dlg)
    {
    }CClientSock::~CClientSock()
    {
    }
    // Do not edit the following lines, which are needed by ClassWizard.
    #if 0
    BEGIN_MESSAGE_MAP(CClientSock, CSocket)
    //{{AFX_MSG_MAP(CClientSock)
    //}}AFX_MSG_MAP
    END_MESSAGE_MAP()
    #endif // 0/////////////////////////////////////////////////////////////////////////////
    // CClientSock member functions//DEL void CClientSock::OnRecive()
    //DEL {
    //DEL  
    //DEL }void CClientSock::OnReceive(int nErrorCode) 
    {
    // TODO: Add your specialized code here and/or call the base class

    CSocket::OnReceive(nErrorCode);
    //if(m_pdlg)
    //{ //}
    }