我知道关键字在封装方面的一般用例,friend但有几次,我需要关键字friend只是为了“完成工作”。这些用例并不让我高兴,所以我想知道是否有一些替代方案。这是第一个最小的例子:
struct Foo{
enum class Bar{
a=1,b=2,c=4
};
// need to tell the compiler of operator| before it gets used
// but it can't be a member function of Foo: so add friend keyword
friend Bar operator|(const Bar& b1, const Bar& b2);
// constructor needs a default value using
// operator| for Bars
Foo( Bar b = Bar::a | Bar::b );
};
// definition of operator|, etc.
Run Code Online (Sandbox Code Playgroud)
在构造函数声明中给出默认值之前,编译器有什么方法可以查看接口内部嵌套类的声明吗?operator|Foo …
c++ templates operator-overloading friend non-member-functions