같은 코드가있다 :액세스 친구 기능은 클래스에 정의
#include <iostream>
class A{
public:
friend void fun(A a){std::cout << "Im here" << std::endl;}
friend void fun2(){ std::cout << "Im here2" << std::endl; }
friend void fun3();
};
void fun3(){
std::cout << "Im here3" << std::endl;
}
int main()
{
fun(A()); // works ok
//fun2(); error: 'fun2' was not declared in this scope
//A::fun2(); error: 'fun2' is not a member of 'A'
fun3(); // works ok
}
어떻게 액세스 할 수있는 기능 Fun2에가()?
+1 : 잘 공식화 된 질문입니다. –
우리는 부스트의 스마트 포인터 인 intrusive_ptr에서 이런 종류의 코드를 보았습니다. 나에게 그것은 그것을 정의하는 것이 의미가 없다. 오히려 범위 내의 어딘가에서 친구를 정의하고 클래스 범위 선언에서 프로토 타입을 친구로 표시한다. 이것은 더 읽기 쉽다. – Gabriel