让自己成为一个朋友

3 c++ friend

编辑:看起来我完全被误导了.请关闭此帖子.嘎.

为了记录,以下编译和工作:

class ForeverAlone
{
private:
  int m_friends;
  HANDLE m_handle;

public:
  ForeverAlone()
  {
    m_handle = CreateThread(NULL, 0, &ForeverAlone::SadThread, reinterpret_cast<void*>(this), 0, NULL);
  }

  ~ForeverAlone()
  {
    if (m_handle != NULL)
      CloseHandle(m_handle);
  }

protected:
  static unsigned long WINAPI SadThread(void* param)
  {
    ForeverAlone* thisObject = reinterpret_cast<ForeverAlone*>(param);

    // is there any way for me to access:
    thisObject->m_friends;
  }
};
Run Code Online (Sandbox Code Playgroud)

原始问题:我有一个静态保护线程方法,我将一个对象传递给它.我可以以某种方式使类friend本身,所以我可以访问其私人成员?

jmu*_*llo 11

所有类方法(静态或非静态)都是类的"朋友".Friend用于允许外部函数和类访问类.班级总是自己的"朋友".