做一个类朋友本身 [英] Making a class friend itself

查看:98
本文介绍了做一个类朋友本身的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

编辑:看起来我完全错误。请关闭此线程。

It looks like I'm completely misinformed. Please close this thread. Gah.

对于记录,以下编译和作品:

For the record, the following compiles and works:

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;
  }
};



原始问题:我有一个静态受保护的线程方法,我传递一个对象。我可以以某种方式使类 friend 本身,以便我可以访问其私有成员?

Original question: I have a static protected thread method, which I pass an object to. Can I somehow make the class friend itself so I can access its private members?

推荐答案

所有类方法,无论是否静态,都是类的朋友。 Friend用于允许外部函数和类访问类。这个类总是自己的朋友。

All class methods, static or not, are automatically "friends" of the class. Friend is used to allow external functions and classes access to a class. The class is always its own "friend".

这篇关于做一个类朋友本身的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆