這並不編譯,這裏發生了什麼?
#include <boost/intrusive_ptr.hpp>
class X
{
public:
void intrusive_ptr_add_ref(X* blah)
{
}
void intrusive_ptr_release(X * blah)
{
}
};
int main()
{
boost::intrusive_ptr<X> ex(new X);
}
但這:
#include <boost/intrusive_ptr.hpp>
class X
{
public:
friend void intrusive_ptr_add_ref(X* blah)
{
}
friend void intrusive_ptr_release(X * blah)
{
}
};
int main()
{
boost::intrusive_ptr<X> ex(new X);
}
這:
#include <boost/intrusive_ptr.hpp>
class X
{
public:
};
void intrusive_ptr_add_ref(X* blah)
{
}
void intrusive_ptr_release(X * blah)
{
}
int main()
{
boost::intrusive_ptr<X> ex(new X);
}
我想它是與SFINAE(這是我的天堂」 t仍然困擾着理解)? friend限定符是否將所定義的函數作爲一個自由函數放在封閉的命名空間中?
編輯
誰刪除其職,成員函數非友爲add_ref
和release
(這些特定的成員函數沒有在documention提到...)解決了問題。使用friend
限定符的嵌套定義會發生什麼情況?
你有什麼錯誤? – 2012-01-06 17:33:44
關於編輯:您使用friend限定符定義的函數不是成員函數,它們是自由函數。他們在課堂上定義的事實並不能使他們成爲成員(我同意這可能會產生誤導)。 – 2012-01-06 17:42:55
至於使用'add_ref'和'release'成員函數而不是免費函數,應該注意的是,這在Boost文檔中沒有明確說明(或者至少我沒有設法找到它)。 – 2012-01-06 17:46:52