声明 C++静态成员函数作为其所在类的友元(语法)

发布于 2024-12-23 16:34:06 字数 538 浏览 8 评论 0原文

将静态成员函数声明为其所在类的友元的语法是什么?

class MyClass
{
private:
  static void Callback(void* thisptr); //Declare static member
  friend static void Callback(void* thisptr); //Define as friend of itself
}

我可以将其折叠成一行吗?

class MyClass
{
private:
  friend static void Callback(void* thisptr); //Declare AND Define as friend
}

是否有另一种方法可以将其全部折叠成一行?

回答

请不要投反对票,这源于我对 C++ 静态成员函数缺乏了解。答案是他们不需要成为朋友,他们已经可以访问私人成员。所以我的问题有点无效。

What is the syntax for declaring a static member function as a friend of the class in which it resides.

class MyClass
{
private:
  static void Callback(void* thisptr); //Declare static member
  friend static void Callback(void* thisptr); //Define as friend of itself
}

Can I fold it into this one-liner?

class MyClass
{
private:
  friend static void Callback(void* thisptr); //Declare AND Define as friend
}

Is there another way to fold it all into a single line?

Answer

Please don't downvote, this stems from my lack of knowledge about C++ static member functions. The answer is that they don't need to be friend, they already can access private members. So my question was somewhat invalid.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

萌梦深 2024-12-30 16:34:06

其实如果是静态的就不用用friend更准确了。静态成员函数可以像普通成员函数一样访问类的内部。唯一的区别是它没有 this 指针。

void MyClass::Callback(void* thisptr) {
    MyClass* p = static_cast<MyClass*>(thisptr);
    p->public_func(); // legal
    p->private_func(); // legal
    p->private_int_var = 0; // legal
}

Actually, no need to use friend if it is static is more accurate. A static member function has access to the internals of the class just like a normal member function. The only difference is it doesn't have a this pointer.

void MyClass::Callback(void* thisptr) {
    MyClass* p = static_cast<MyClass*>(thisptr);
    p->public_func(); // legal
    p->private_func(); // legal
    p->private_int_var = 0; // legal
}
柳絮泡泡 2024-12-30 16:34:06

类成员函数不能是其自己类的友元 - 它已经是类成员并且可以访问其私有函数。和它交朋友有什么意义?这不是脸书...

The class member function cannot be a friend of its own class - its already the class member and can access its privates. What' the point in befriending it? Its not Facebook...

缱倦旧时光 2024-12-30 16:34:06

默认情况下,静态成员函数可以访问类的protected / private部分,无需将其设为friend

#include <iostream>

struct Foo{
  Foo(int i) : an_int(i) {}

  static void print_an_int(Foo& self){
    std::cout << self.an_int;
  }
private:
  int an_int;
};

int main(){
  Foo f(5);
  Foo::print_an_int(f); // output: 5
}

A static member function has access to the protected / private parts of a class by default, no need to make it a friend.

#include <iostream>

struct Foo{
  Foo(int i) : an_int(i) {}

  static void print_an_int(Foo& self){
    std::cout << self.an_int;
  }
private:
  int an_int;
};

int main(){
  Foo f(5);
  Foo::print_an_int(f); // output: 5
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文