9
如何讓像這樣的嵌套私人類的運營商< <超載?爲嵌套私有類重載operator <<可能嗎?
class outer {
private:
class nested {
friend ostream& operator<<(ostream& os, const nested& a);
};
// ...
};
當試圖外部類的編譯器之外抱怨隱私:
error: ‘class outer::nested’ is private
和TH e打印函數應該是'const'成員函數。如果它是'std :: ostream&print(std :: ostream&out)const',那麼'operator <<'可以只是一行:'return obj.print(dest);'。 – Nawaz
爲什麼std:ostream需要成爲Inner的朋友? – Mordachai
因爲你不能在'Inner'中定義它,否則。 (如果它沒有在'Inner'中定義,那麼你有問題,它不能訪問'Inner',因爲'Inner'是'Outer'的私有成員。) –