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'的私有成员。) –