10

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.

unixman83
  • 9,421
  • 10
  • 68
  • 102
  • No need to use static if it is friend. – KV Prajapati Dec 31 '11 at 02:04
  • @AVD then it cannot be called by C. That's the reason it's static in the first place. – unixman83 Dec 31 '11 at 02:06
  • @unixmax83: it can't be called by C anyway because it also needs to be declared extern "C". Although most compiler allow calling a functions without this from C and it even works, it isn't portable and there are system which have different calling conventions for C and C++ functions. – Dietmar Kühl Dec 31 '11 at 02:09

3 Answers3

7

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
}
jmucchiello
  • 18,754
  • 7
  • 41
  • 61
  • I was using `reinterpret_cast` thanks for correcting me. – unixman83 Dec 31 '11 at 02:45
  • There's nothing wrong with reinterpret_cast here. Casting from void*, static_cast and reinterpret_cast should produce identical code based on most common C++ implementations. – jmucchiello Dec 31 '11 at 05:14
2

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...

littleadv
  • 20,100
  • 2
  • 36
  • 50
2

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
}
Xeo
  • 129,499
  • 52
  • 291
  • 397
  • Just to point out: the function being static is entirely irrelevant. **Any** class member function can access the class private/protected parts. – littleadv Dec 31 '11 at 02:13
  • 1
    @littleadv: That's pretty obvious, the OP's misunderstanding seemed to be exactly this, that static member functions aren't treated any differently than normal member function w.r.t. access checking. – Xeo Dec 31 '11 at 02:14
  • not the answer I was looking for. jmucchiello shows an example with a pointer, your answer is by reference. – unixman83 Dec 31 '11 at 02:39
  • @unixman: Erm... that doesn't matter in any way, the concept is still the same. Static class member -> has access to protected/private content. – Xeo Dec 31 '11 at 10:29