繁体   English   中英

如何给朋友模板类功能

[英]How to friend template class function

我试图以朋友的身份在模板类中声明一个特定的函数。

template <class T>
class A
{
    // Constructor, destructor, etc..

    void update();
}

// This is the function I'm trying to declare as a friend
template <class T> void A<T>::update()
{
    // Do stuff, accessing private members of B
}

我正在尝试提供朋友访问权限的课程:

class B
{
// Here is where I can't figure out how to declare A::update() as a friend function
}

我能够通过以下方式成功地将A全部声明为朋友:

// Forward declare at top
template class<T>
class A;

// Then inside of class B
template <class T> friend class A;

但是,我试图仅在A :: update()内限制对B的私有/受保护范围的访问。

尝试这个:

template <class T>
class A
{
    void update();
};

class B
{
private:
    int asd;
    template <class T> friend void A<T>::update();
};

template <class T> void A<T>::update()
{
    B test;
    test.asd = 2;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM