重载运算符<<嵌套的私有类可能吗? [英] Overloading operator<< for a nested private class possible?
本文介绍了重载运算符<<嵌套的私有类可能吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何使操作员超负荷<<像这样的嵌套私人班级?
How one can overload an operator<< for a nested private class like this one?
class outer {
private:
class nested {
friend ostream& operator<<(ostream& os, const nested& a);
};
// ...
};
在外部类之外尝试时,编译器会抱怨隐私:
When trying outside of outer class compiler complains about privacy:
error: ‘class outer::nested’ is private
推荐答案
您也可以将operator<<
设为outer
的朋友.或者你
可以在nested
中完全实现inline
,例如:
You could make the operator<<
a friend of outer
as well. Or you
could implement it completely inline
in nested
, e.g.:
class Outer
{
class Inner
{
friend std::ostream&
operator<<( std::ostream& dest, Inner const& obj )
{
obj.print( dest );
return dest;
}
// ...
// don't forget to define print (which needn't be inline)
};
// ...
};
这篇关于重载运算符<<嵌套的私有类可能吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文