為什麼C 友元類宣告後,引用友元類的地方還會出現報錯?

時間 2021-06-06 18:05:42

1樓:d41d8c

GCC和Clang遵守了[namespace.memdef]:

If a friend declaration in a non-local class first declares a class, function, class template or function template the friend is a member of the innermost enclosing namespace.

The friend declaration does not by itself make the name visible to unqualified lookup or qualified lookup

.簡而言之雖然friend class B;是個宣告,但並沒有使B可見(visible),所以名稱查詢(name lookup)仍然找不到它們。——換言之就是雖然宣告了,但是用不了。

cppreference其實在後面的段落也提到了:

A name first declared in a friend declaration within class or class template X becomes a member of the innermost enclosing namespace of X, but is not visible for lookup (except argument-dependent lookup that considers X) unless a matching declaration at the namespace scope is provided - see namespaces for details.

C 類當中為什麼要有private?

麻辣牛肉 為什麼要有C 到了彙編全都是記憶體位址,暫存器和運算,C不就很好了嗎?還是說直接彙編就ok了?什麼虛函式,引用,const,統統不過是語法糖罷了,到了彙編,還不是乙個樣 Ronaldo 因為private就是要在編譯期間限制你這個類成員只能這個類自己用,其他類別用,也別操心。用了就給你報錯...

C 中為什麼要有allocator類?

Wu Jarvis 我是這麼理解的 其實你只要分清vector中resize,reserve的關係就能明白了,resize時,當元素數量 capacity時不但會分配空間,而且會初始化元素 reserve只會做分配空間的事情,不會做初始化,這樣就把分配空間和初始化的事情分開來了,如果不分開的話,那麼...

C 為什麼Random類不做靜態?

程子昂 這保證每個Random例項都是乙個固定的偽隨機數生成器 PRNG 也就是說兩個Random例項如果用同乙個隨機數種子的話生成的隨機數序列是一樣的 預設的種子是系統時間 這個性質在某些地方會很有用,譬如密碼學裡的對稱流加密就可以用PRNG實現 總這個角度講Random其實也不太安全,RNGCy...