我碰到下面的代碼好友功能和實現
class ExDer1 : public ExBase
{
public:
friend int Der1Fn()
{
....
}
};
來到我與
friend int Der1Fn()
{
//This has an implementation .Why is it a friend then ? since it can access the private/protected variables of the ExDer1 class ?
}
通常有點困惑在這裏,我希望看到類似下面的
friend int Der1Fn(); //No implementation. Indicating that the Der1Fn is a method outside this class
這基本上意味着功能int Der1Fn()
將訪問的私有變量的類ExDer1。不過這有一個實現。請有人解釋這是什麼意思嗎?
更新:
所以,如果我有以下代碼
class ExDer1 : public ExBase
{
public:
friend int Der1Fn()
{
std::cout << "Hello World";
}
};
int main()
{
Der1Fn(); // error C3767: 'Der1Fn': candidate function(s) not accessible
//.....
}
我怎麼叫Der1Fn?
你從哪裏知道你不能在課堂上提供朋友功能的實現? – P0W 2014-09-23 07:53:54
我查看了http://msdn.microsoft.com/en-us/library/h2x4fzdz.aspx上的示例,他們沒有任何聲明爲friend的實現的示例。 – Rajeshwar 2014-09-23 07:55:01
這意味着這是該功能的實施。你不明白什麼? – 2014-09-23 07:57:10