我在下面的代码中看到了一些我无法解释的内容.在VS6,VS9和GCC T2 :: foo2()下给出错误:'bar':无法访问在类'C1'中声明的受保护成员.但是如果你删除C1 :: bar(),它会编译并正确运行,即使T2仍在访问受保护的C1B:bar(),你会认为这是同样的问题.
注意,在T2 :: foo2()中,您可以将'pT1'强制转换为'T1*'并且一切正常,但这仍然无法解释为什么允许C1B :: bar(),但C1 :: bar( ) 不是.
template<class S> class T2;
template<class T> class T1
{
//template<class T> friend class T2; --> this doesn't compile under VS6
friend class T2<T>;
protected:
virtual void bar() { printf("T1\n"); }
};
template<class S> class T2
{
public:
void foo1(T1<S> *pT1) { pT1->bar(); } // --> ok, makes sense, this works either way
void foo2(S *pT1) { pT1->bar(); } // --> this fails to compile if …Run Code Online (Sandbox Code Playgroud)