C ++类成员访问模板的问题

时间:2009-10-21 07:03:54

标签: c++ templates

我遇到了一个问题,如果我有一个模板类,而模板类又有一个模板方法,它接受该类的另一个实例的参数(具有不同的模板参数),它无法访问受保护的或私有的成员作为参数传递的类,例如:

template<typename T>class MyClass
{
    T v;
public:
    MyClass(T v):v(v){}

    template<typename T2>void foo(MyClass<T2> obj)
    {
        std::cout << v     << " ";
        //error C2248: 'MyClass<T>::v' : cannot access private member declared in class 'MyClass<T>'
        std::cout << obj.v << " ";
        std::cout << v + obj.v << std::endl;
    }
};
int main()
{
    MyClass<int> x(5);
    MyClass<double> y(12.3);
    x.foo(y);
}

是否有人说MyClass&lt; T&gt;中的方法了?可以完全访问MyClass&lt; SomeOtherT&gt;?

2 个答案:

答案 0 :(得分:11)

它们是不同的类型:模板从模板构建类型。

你必须对你的班级朋友进行其他实例化:

template <typename T>class MyClass
{
    T v;
public:
    MyClass(T v):v(v){}

    template<typename T2>void foo(MyClass<T2> obj)
    {
        std::cout << v     << " ";
        std::cout << obj.v << " ";
        std::cout << v + obj.v << std::endl;
    }

    // Any other type of MyClass is a friend.
    template <typename U>
    friend class MyClass;

    // You can also specialize the above:
    friend class MyClass<int>; // only if this is a MyClass<int> will the
                               // other class let us access its privates
                               // (that is, when you try to access v in another
                               // object, only if you are a MyClass<int> will
                               // this friend apply)
};

答案 1 :(得分:7)

MyClass添加为朋友类:

template<typename T> class MyClass
{
    template<typename TX>
    friend class MyClass;
 ...

根据C ++标准14.5.3 / 3:

  

可以在类或类模板中声明朋友模板。可以在类或类模板中定义朋友函数模板,但是可以不在类或类模板中定义朋友类模板。在这些情况下,朋友类或朋友功能模板的所有特化都是授予友谊的类或类模板的朋友。 [实施例:

class A {
  template<class T> friend class B;  // OK
  template<class T> friend void f(T){ /* ... */ }  // OK
};
     

-end example]

注意:您应该知道上面的代码仍然可能导致某些编译器出错,因为Core Issue #602仍处于打开状态。尽管如此,上面的代码编译在GCC,Visual C ++和Comeau上。

要仅为朋友foo打电话,您可以写下以下内容:

template<typename T> class MyClass
{
    template<typename TY> template<typename TX> 
    friend void MyClass<TY>::foo(MyClass<TX>);
 ...